編輯

共用方式為


HtmlSelectBuilder Class

Definition

Interacts with the parser to build an HtmlSelect control.

public ref class HtmlSelectBuilder : System::Web::UI::ControlBuilder
public class HtmlSelectBuilder : System.Web.UI.ControlBuilder
type HtmlSelectBuilder = class
    inherit ControlBuilder
Public Class HtmlSelectBuilder
Inherits ControlBuilder
Inheritance
HtmlSelectBuilder

Examples

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

Remarks

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.

Constructors

HtmlSelectBuilder()

Initializes a new instance of the HtmlSelectBuilder class.

Properties

BindingContainerBuilder

Gets the control builder that corresponds to the binding container for the control that this builder creates.

(Inherited from ControlBuilder)
BindingContainerType

Gets the type of the binding container for the control that this builder creates.

(Inherited from ControlBuilder)
ComplexPropertyEntries

Gets a collection of complex property entries.

(Inherited from ControlBuilder)
ControlType

Gets the Type for the control to be created.

(Inherited from ControlBuilder)
CurrentFilterResolutionService

Gets an IFilterResolutionService object that is used to manage device-filter related services when parsing and persisting controls in the designer.

(Inherited from ControlBuilder)
DeclareType

Gets the type that will be used by code generation to declare the control.

(Inherited from ControlBuilder)
FChildrenAsProperties

Gets a value that determines whether the control has a ParseChildrenAttribute with ChildrenAsProperties set to true.

(Inherited from ControlBuilder)
FIsNonParserAccessor

Gets a value that determines whether the control implements the IParserAccessor interface.

(Inherited from ControlBuilder)
HasAspCode

Gets a value indicating whether the control contains any code blocks.

(Inherited from ControlBuilder)
ID

Gets or sets the identifier property for the control to be built.

(Inherited from ControlBuilder)
InDesigner

Returns whether the ControlBuilder is running in the designer.

(Inherited from ControlBuilder)
InPageTheme

Gets a Boolean value indicating whether this ControlBuilder object is used to generate page themes.

(Inherited from ControlBuilder)
ItemType

Gets the type set on the binding container.

(Inherited from ControlBuilder)
Localize

Gets a Boolean value indicating whether the control that is created by this ControlBuilder object is localized.

(Inherited from ControlBuilder)
NamingContainerType

Gets the type of the naming container for the control that this builder creates.

(Inherited from ControlBuilder)
PageVirtualPath

Gets the virtual path of a page to be built by this ControlBuilder instance.

(Inherited from ControlBuilder)
Parser

Gets the TemplateParser responsible for parsing the control.

(Inherited from ControlBuilder)
ServiceProvider

Gets the service object for this ControlBuilder object.

(Inherited from ControlBuilder)
SubBuilders

Gets a list of child ControlBuilder objects for this ControlBuilder object.

(Inherited from ControlBuilder)
TagName

Gets the tag name for the control to be built.

(Inherited from ControlBuilder)
TemplatePropertyEntries

Gets a collection of template property entries.

(Inherited from ControlBuilder)
ThemeResolutionService

Gets an IThemeResolutionService object that is used in design time to manage control themes and skins.

(Inherited from ControlBuilder)

Methods

AllowWhitespaceLiterals()

Determines whether the white space literals in an HtmlSelect control are to be processed or ignored.

AppendLiteralString(String)

Adds the specified literal content to a control. This method is called by the ASP.NET page framework.

(Inherited from ControlBuilder)
AppendSubBuilder(ControlBuilder)

Adds builders to the ControlBuilder object for any child controls that belong to the container control.

(Inherited from ControlBuilder)
BuildObject()

Builds a design-time instance of the control that is referred to by this ControlBuilder object.

(Inherited from ControlBuilder)
CloseControl()

Called by the parser to inform the builder that the parsing of the control's opening and closing tags is complete.

(Inherited from ControlBuilder)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetChildControlType(String, IDictionary)

Obtains the Type for the HtmlSelect control's child controls.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObjectPersistData()

Creates the ObjectPersistData object for this ControlBuilder object.

(Inherited from ControlBuilder)
GetResourceKey()

Retrieves the resource key for this ControlBuilder object.

(Inherited from ControlBuilder)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HasBody()

Determines if a control has both an opening and closing tag. This method is called by the ASP.NET page framework.

(Inherited from ControlBuilder)
HtmlDecodeLiterals()

Determines whether the literal string of an HTML control must be HTML decoded. This method is called by the ASP.NET page framework.

(Inherited from ControlBuilder)
Init(TemplateParser, ControlBuilder, Type, String, String, IDictionary)

Initializes the ControlBuilder for use after it is instantiated. This method is called by the ASP.NET page framework.

(Inherited from ControlBuilder)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
NeedsTagInnerText()

Determines if the control builder needs to get its inner text. If so, the SetTagInnerText(String) method must be called. This method is called by the ASP.NET page framework.

(Inherited from ControlBuilder)
OnAppendToParentBuilder(ControlBuilder)

Notifies the ControlBuilder that it is being added to a parent control builder.

(Inherited from ControlBuilder)
ProcessGeneratedCode(CodeCompileUnit, CodeTypeDeclaration, CodeTypeDeclaration, CodeMemberMethod, CodeMemberMethod)

Enables custom control builders to access the generated Code Document Object Model (CodeDom) and insert and modify code during the process of parsing and building controls.

(Inherited from ControlBuilder)
SetResourceKey(String)

Sets the resource key for this ControlBuilder object.

(Inherited from ControlBuilder)
SetServiceProvider(IServiceProvider)

Sets the service object for this ControlBuilder object.

(Inherited from ControlBuilder)
SetTagInnerText(String)

Provides the ControlBuilder with the inner text of the control tag.

(Inherited from ControlBuilder)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also