Export (0) Print
Expand All

HtmlSelectBuilder Class

Interacts with the parser to build an HtmlSelect control.

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

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

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

	}

}

System.Object
  System.Web.UI.ControlBuilder
    System.Web.UI.HtmlControls.HtmlSelectBuilder

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

Community Additions

ADD
Show:
© 2014 Microsoft