自定义控件:
1.在BLL层(或其他层---n层结构)右键-〉add -〉Custom Control->groupbox.cs
2.开始编写自定义控件,命名空间不是可选的,控件类必须属于某个命名空间。文件顶端的using 语句指定了----包含该控件所使用的类型的----命名空间。namespace语句将类的定义放入一个名为BLL的自定义命名空间。
3.csc /t:library /out:groupbox.dll groupbox.cs 生成groupbox.dll文件
4.把groupbox.dll文件拷贝至UI层的bin文件夹内。
5.在groupboxPage.aspx页面首部写:
<%@Register TagPrefix="win" Namespace="BLL" Assembly="groupbox.dll">
关键是Namespace和Assembly要和groupbox.cs 中对应,groupbox.dll即是第三步生成的groupbox.dll
6.在groupboxPage.aspx页面内就可以写:
<win:groupbox ID="MyGroupBox" Text="Colors" runat="server">
.........................关键是RadioButtonList不要放到外面去,要放到groupbox的里面来
</win:groupbox>
groupbox.cs:
using System;
using System.Web;
using System.Web.UI;
namespace BLL
{
public partial class GroupBox : Control
{
/*
public GroupBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
// Calling the base class OnPaint
base.OnPaint(pe);
}
*/
string MyText = "";
public string Text
{
get { return MyText; }
set { MyText = value; }
}
protected override void Render(HtmlTextWriter writer)
{
//输出一个<fieldset标记
writer.WriteBeginTag("fieldset");//<fieldset id=
if ( ID != null)
{
writer.WriteAttribute("id", ClientID);
//represent the closing angle bracket(>) of a markup tag
writer.Write(HtmlTextWriter.TagRightChar);
}
//输出一个<legend>元素
if (Text.Length > 0)
{
writer.WriteFullBeginTag("legend");//<legend>
writer.Write(Text);
writer.WriteEndTag("legend");
}
//输出<fieldset></fieldset>标记之间的内容
base.Render(writer);
//输出一个</fieldset>标记
writer.WriteEndTag("fieldset");
}
}
}
using System.Web;
using System.Web.UI;
namespace BLL
{
public partial class GroupBox : Control
{
/*
public GroupBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
// Calling the base class OnPaint
base.OnPaint(pe);
}
*/
string MyText = "";
public string Text
{
get { return MyText; }
set { MyText = value; }
}
protected override void Render(HtmlTextWriter writer)
{
//输出一个<fieldset标记
writer.WriteBeginTag("fieldset");//<fieldset id=
if ( ID != null)
{
writer.WriteAttribute("id", ClientID);
//represent the closing angle bracket(>) of a markup tag
writer.Write(HtmlTextWriter.TagRightChar);
}
//输出一个<legend>元素
if (Text.Length > 0)
{
writer.WriteFullBeginTag("legend");//<legend>
writer.Write(Text);
writer.WriteEndTag("legend");
}
//输出<fieldset></fieldset>标记之间的内容
base.Render(writer);
//输出一个</fieldset>标记
writer.WriteEndTag("fieldset");
}
}
}
groupboxPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GroupBoxPage.aspx.cs" Inherits="GroupBoxPage" %>
<%@Register TagPrefix="win" Namespace="BLL" Assembly="GroupBox" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="Toggle" AutoPostBack="true" Text="Show Color" OnCheckedChanged="OnToggle" Checked="true" runat="server" />
<br />
<win:GroupBox ID="MyGroupBox" Text="Colors" runat="server">
<asp:RadioButtonList ID="rbl1" runat="server">
<asp:ListItem Text="red" Selected="true"></asp:ListItem>
<asp:ListItem Text="Green" ></asp:ListItem>
<asp:ListItem Text="Blue"></asp:ListItem>
</asp:RadioButtonList>
</win:GroupBox>
</form>
</body>
</html>
<%@Register TagPrefix="win" Namespace="BLL" Assembly="GroupBox" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="Toggle" AutoPostBack="true" Text="Show Color" OnCheckedChanged="OnToggle" Checked="true" runat="server" />
<br />
<win:GroupBox ID="MyGroupBox" Text="Colors" runat="server">
<asp:RadioButtonList ID="rbl1" runat="server">
<asp:ListItem Text="red" Selected="true"></asp:ListItem>
<asp:ListItem Text="Green" ></asp:ListItem>
<asp:ListItem Text="Blue"></asp:ListItem>
</asp:RadioButtonList>
</win:GroupBox>
</form>
</body>
</html>