支持页分析器生成控件及其包含的子控件。
有关此类型所有成员的列表,请参阅 ControlBuilder 成员。
System.Object
System.Web.UI.ControlBuilder
派生类
[Visual Basic] Public Class ControlBuilder [C#] public class ControlBuilder [C++] public __gc class ControlBuilder [JScript] public class ControlBuilder
线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。
备注
默认情况下,页上的每个控件都与一个默认的 ControlBuilder 类关联。对于在自定义控件标记中遇到的每个嵌套控件,此类将一个子控件添加到 Controls 集合。此外,它为位于嵌套控件标记之间的文本创建文本控件。您可以通过定义自己的自定义控件生成器类来重写此默认行为。具体的方法是将 ControlBuilderAttribute 应用到您的控件生成器类,如下所示:[ControlBuilderAttribute(typeof(ControlBuilderType))]
示例
[Visual Basic, C#, C++] 下面的示例创建一个 Table 控件,它的属性和内容在生成此表时定义。以下是用于生成可执行文件的命令行。
vbc /r:System.dll /r:System.Web.dll /r:System.Drawing.dll /t:library /out:myWebAppPath/bin/vb_mycontrolbuilder.dll myControlBuilder.vb
csc /t:library /out:myWebAppPath/bin/cs_mycontrolbuilder.dll myControlBuilder.cs
[C#] /* File name: myControlBuilder.cs. */ using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.Drawing; namespace CustomControls { public class MyTableCell : TableCell, INamingContainer {}; public class MyCell /* * Class name: MyCell. * Declares the class for the child controls to include in the control collection. */ { string _id; string _value; Color _backColor; public string CellID { get {return _id;} set {_id = value;} } public string Text { get {return _value;} set {_value = value;} } public Color BackColor { get {return _backColor;} set { _backColor = value;} } }; public class MyControlBuilder : ControlBuilder { [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] public override Type GetChildControlType(string tagName, IDictionary attribs) { // Allows TableRow without "runat=server" attribute to be added to the collection. if (tagName.ToLower().EndsWith("mycell")) return typeof(MyCell); return null; } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] public override void AppendLiteralString(string s) { // Ignores literals between rows. } } [ControlBuilderAttribute(typeof(MyControlBuilder))] public class MyCS_CustomControl: Control, INamingContainer /* * Class name: MyCS_CustomControl. * Processes the element declarations within a control declaration. * This builds the actual control. */ { // Declares the custom control that must be built programmatically. Table _table; private String _title; private int _rows; private int _columns; Hashtable _cellObjects = new Hashtable(); // Rows property to be used as the attribute name in the Web page. public int Rows { get {return _rows;} set {_rows = value;} } // Columns property to be used as the attribute name in the Web page. public int Columns { get {return _columns;} set {_columns = value;} } // Title property to be used as the attribute name in the Web page. public string Title { get {return _title;} set {_title = value;} } protected void createNewRow( int rowNumber ) { // Creates a row and adds it to the table. TableRow row; row = new TableRow(); _table.Rows.Add( row ); // Creates a cell that contains text. for( int y=0; y < Columns ; y++ ) appendCell( row, rowNumber, y ); } protected void appendCell( TableRow row, int rowNumber, int cellNumber ) { TableCell cell; TextBox textbox; cell = new TableCell(); textbox = new TextBox(); cell.Controls.Add( textbox ); textbox.ID = "r" + rowNumber.ToString() + "c" + cellNumber.ToString(); // Checks for the MyCell child object. if ( _cellObjects[textbox.ID] != null ) { MyCell cellLookup; cellLookup = (MyCell) _cellObjects[textbox.ID]; textbox.Text = cellLookup.Text; textbox.BackColor = cellLookup.BackColor; } else textbox.Text = "Row: " + rowNumber.ToString() + " Cell: " + cellNumber.ToString(); row.Cells.Add( cell ); } // Called at runtime when a child object is added to the collection. [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void AddParsedSubObject(object obj) { MyCell cell = obj as MyCell; if (cell != null) { _cellObjects.Add( cell.CellID, cell ); } } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void OnPreRender(EventArgs e) /* * Function name: OnPreRender. * Carries out changes affecting the control state and renders the resulting UI. */ { // Increases the number of rows if needed. while (_table.Rows.Count < Rows) { createNewRow(_table.Rows.Count ); } // Checks that each row has the correct number of columns. for (int i=0; i<_table.Rows.Count; i++) { while(_table.Rows[i].Cells.Count<Columns) { appendCell(_table.Rows[i], i, _table.Rows[i].Cells.Count); } while(_table.Rows[i].Cells.Count>Columns) { _table.Rows[i].Cells.RemoveAt(_table.Rows[i].Cells.Count - 1); } } } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void CreateChildControls() /* * Function name: CreateChildControls. * Adds the Table and the text control to the control collection. */ { LiteralControl text; // Initializes the text control using the Title property. text = new LiteralControl("<h5>" + Title + "</h5>"); Controls.Add(text); _table = new Table(); _table.BorderWidth = 2; Controls.Add(_table); } } } [Visual Basic, C#, C++] 下面的示例使用前面的自定义控件。具体说来,它生成一个属性和内容在运行时定义的表。注意 Register 指令中显示的值反映上一个命令行。
[C#]
<%@ Register TagPrefix="custom" Assembly="cs_mycontrolbuilder" Namespace="CustomControls" %>
<html>
<body>
<h4>Using the ControlBuilder Class</h4>
<form runat="server">
<custom:MyCS_CustomControl ID="csTableId" rows="2" columns="2" Title="C# Custom Control Table" runat=server>
<custom:MyCell CellID="r0c0" BackColor="red" Text="Hello"/>
<custom:MyCell CellID="r0c1" BackColor="yellow" Text="Customer,"/>
<custom:MyCell CellID="r1c0" BackColor="aqua" Text="How Are"/>
<custom:MyCell CellID="r1c1" BackColor="magenta" Text="You?"/>
</custom:MyCS_CustomControl>
</form>
</body>
</html>
要求
命名空间: System.Web.UI
平台: Windows 2000, Windows XP Professional, Windows Server 2003 系列
程序集: System.Web (在 System.Web.dll 中)
浙公网安备 33010602011771号