Export (0) Print
Expand All

HtmlSelectBuilder Class

Note: This class is new in the .NET Framework version 2.0.

Interacts with the parser to build an HtmlSelect control.

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

'Declaration
Public Class HtmlSelectBuilder
	Inherits ControlBuilder
'Usage
Dim instance As HtmlSelectBuilder

public class HtmlSelectBuilder extends ControlBuilder
public class HtmlSelectBuilder extends 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="VB"%>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>

<!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>

Imports System
Imports System.Security.Permissions
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Namespace Samples.AspNet.VB.Controls
    ' Define a type of child control for the custom HtmlSelect control.
    Public Class MyOption1
        Private _id As String
        Private _value As String
        Private _text As String


        Public Property optionid() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        Public Property value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property

        Public Property [text]() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property
    End Class 

    ' Define a type of child control for the custom HtmlSelect control.
    Public Class MyOption2
        Private _id As String
        Private _value As String
        Private _text As String


        Public Property optionid() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        Public Property value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property

        Public Property [text]() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property
    End Class 

    ' Define a custom HtmlSelectBuilder control.
    Public Class MyHtmlSelectBuilder
        Inherits HtmlSelectBuilder

        <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
        Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attribs As IDictionary) As Type

            ' Distinguish between two possible types of child controls.
            If tagName.ToLower().EndsWith("myoption1") Then
                Return GetType(MyOption1)
            ElseIf tagName.ToLower().EndsWith("myoption2") Then
                Return GetType(MyOption2)
            End If
            Return Nothing

        End Function 
    End Class 

    <ControlBuilderAttribute(GetType(MyHtmlSelectBuilder))> _
    Public Class CustomHtmlSelect
        Inherits HtmlSelect

        ' Override AddParsedSubObject to treat the two types
        ' of child controls differently.
        Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
            Dim _outputtext As String
            If TypeOf obj Is MyOption1 Then
                _outputtext = "option group 1: " + CType(obj, MyOption1).text
                Dim li As New ListItem(_outputtext, CType(obj, MyOption1).value)
                MyBase.Items.Add(li)
            End If
            If TypeOf obj Is MyOption2 Then
                _outputtext = "option group 2: " + CType(obj, MyOption2).text
                Dim li As New ListItem(_outputtext, CType(obj, MyOption2).value)
                MyBase.Items.Add(li)
            End If

        End Sub 
    End Class 
End Namespace

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 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.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft