Export (0) Print
Expand All

ControlBuilder Class

Supports the page parser in building a control and the child controls it contains.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ControlBuilder

By default, every control on a page is associated with a default ControlBuilder class. During parsing, the ASP.NET page framework builds a tree of ControlBuilder objects corresponding to the tree of controls for the page. The ControlBuilder tree is then used to generate page code to create the control tree. In addition to child controls, the ControlBuilder defines the behavior of how the content within control tags is parsed. You can override this default behavior by defining your own custom control builder class. This is done by applying a ControlBuilderAttribute attribute to your control builder class as follows:

[ControlBuilderAttribute(typeof(ControlBuilderType))]

The following code example creates a Table control whose attributes and content are defined at the time the table is built. The following is the command line to use to build the executable.

[Visual Basic]

vbc /r:System.dll /r:System.Web.dll /r:System.Drawing.dll /t:library /out:myWebAppPath/Bin/vb_mycontrolbuilder.dll myControlBuilder.vb

[C#]

csc /t:library /out:myWebAppPath/Bin/cs_mycontrolbuilder.dll myControlBuilder.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Drawing;
using System.Security.Permissions;

namespace CustomControls
{
	[AspNetHostingPermission(SecurityAction.Demand,
		Level = AspNetHostingPermissionLevel.Minimal)]
	public class MyTableCell : TableCell, INamingContainer { };

	[AspNetHostingPermission(SecurityAction.Demand,
		Level = AspNetHostingPermissionLevel.Minimal)]
	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; }
		}
	};

	[AspNetHostingPermission(SecurityAction.Demand,
	Level = AspNetHostingPermissionLevel.Minimal)]
	public class MyControlBuilder : ControlBuilder
	{

		public override Type GetChildControlType(string tagName, IDictionary attribs)
		{
			// Allows TableRow without "runat=server" attribute to be added to the collection.
			if (String.Compare(tagName, "mycell", true) == 0)
				return typeof(MyCell);
			return null;
		}

		public override void AppendLiteralString(string s)
		{
			// Ignores literals between rows.
		}

	}
	[AspNetHostingPermission(SecurityAction.Demand,
		Level = AspNetHostingPermissionLevel.Minimal)]
	[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.   
		protected override void AddParsedSubObject(object obj)
		{
			MyCell cell = obj as MyCell;
			if (cell != null)
			{
				_cellObjects.Add(cell.CellID, cell);
			}

		}

		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);
				}
			}
		}

		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);
		}
	}
}

The following code example uses the previous custom control. In particular, it builds a table whose attributes and content are defined at run time. Notice that the values shown in the @ Register directive reflect the previous command line.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="AspNetSamples" Assembly="cs_mycontrolbuilder" Namespace="CustomControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ControlBuilder Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <AspNetSamples:MyCS_CustomControl id="Custom1" 
                                         title="Auto-Generated Table"
                                         columns="3"
                                         rows="3"  
                                         runat="server">
         <mycell cellid="r0c0" BackColor="red" text="red cell"></mycell>
         <mycell cellid="r2c2" BackColor="green" text="green cell"></mycell>
       </AspNetSamples:MyCS_CustomControl>
    </div>
    </form>
</body>
</html>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft