HtmlSelectBuilder Class
Assembly: System.Web (in system.web.dll)
The HtmlSelectBuilder control interacts with the page parser to build an HtmlSelect control. Use the HtmlSelectBuilder control to customize the parsing of an HtmlSelect control.
The AllowWhitespaceLiterals property is set to false so that white space is always ignored. Use the GetChildControlType method to determine the type of the HtmlSelect control's child controls.
Notes to Inheritors To create a custom control builder for an HtmlSelect control, you need to inherit from this class.The following code example demonstrates how to create a custom HtmlSelectBuilder control that defines two types of <option> child elements of a custom HtmlSelect control and then processes each type differently.
<%@ Page Language="C#"%> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>HtmlSelectBuilder Example</title> </head> <body> <form id="Form1" runat="server"> <h3>HtmlSelectBuilder Example</h3> <aspSample:CustomHtmlSelect id="customhtmlselect1" runat="server"> <aspSample:MyOption1 optionid="option1" value="1" text="item 1"/> <aspSample:MyOption1 optionid="option2" value="2" text="item 2"/> <aspSample:MyOption2 optionid="option3" value="3" text="item 3"/> <aspSample:MyOption2 optionid="option4" value="4" text="item 4"/> </aspSample:CustomHtmlSelect> </form> </body> </html>
using System; using System.Security.Permissions; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Samples.AspNet.CS.Controls { // Define a type of child control for the custom HtmlSelect control. public class MyOption1 { string _id; string _value; string _text; public string optionid { get { return _id; } set { _id = value; } } public string value { get { return _value; } set { _value = value; } } public string text { get { return _text; } set { _text = value; } } } // Define a type of child control for the custom HtmlSelect control. public class MyOption2 { string _id; string _value; string _text; public string optionid { get { return _id; } set { _id = value; } } public string value { get { return _value; } set { _value = value; } } public string text { get { return _text; } set { _text = value; } } } // Define a custom HtmlSelectBuilder control. public class MyHtmlSelectBuilder : HtmlSelectBuilder { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public override Type GetChildControlType(string tagName, IDictionary attribs) { // Distinguish between two possible types of child controls. if (tagName.ToLower().EndsWith("myoption1")) { return typeof(MyOption1); } else if (tagName.ToLower().EndsWith("myoption2")) { return typeof(MyOption2); } return null; } } [ControlBuilderAttribute(typeof(MyHtmlSelectBuilder))] public class CustomHtmlSelect : HtmlSelect { // Override AddParsedSubObject to treat the two types // of child controls differently. protected override void AddParsedSubObject(object obj) { string _outputtext; if (obj is MyOption1) { _outputtext = "option group 1: " + ((MyOption1)obj).text; ListItem li = new ListItem(_outputtext, ((MyOption1)obj).value); base.Items.Add(li); } if (obj is MyOption2) { _outputtext = "option group 2: " + ((MyOption2)obj).text; ListItem li = new ListItem(_outputtext, ((MyOption2)obj).value); base.Items.Add(li); } } } }
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.