HtmlSelectBuilder 类

定义

与分析器进行交互以生成 HtmlSelect 控件。

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
继承
HtmlSelectBuilder

示例

下面的代码示例演示如何创建自定义控件,该控件定义自定义HtmlSelectBuilderHtmlSelect控件的<option>两种类型的子元素,然后以不同的方式处理每种类型。

<%@ 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

注解

HtmlSelectBuilder 控件与页面分析程序交互以生成控件 HtmlSelect 。 使用 HtmlSelectBuilder 控件自定义控件分析 HtmlSelect

属性 AllowWhitespaceLiterals 设置为 false 允许始终忽略空格。 使用该方法 GetChildControlType 确定控件的 HtmlSelect 子控件的类型。

继承者说明

若要为 HtmlSelect 控件创建自定义控件生成器,需要从此类继承。

构造函数

HtmlSelectBuilder()

初始化 HtmlSelectBuilder 类的新实例。

属性

BindingContainerBuilder

获取与此生成器创建的控件的绑定容器对应的控件生成器。

(继承自 ControlBuilder)
BindingContainerType

获取此生成器所创建控件的绑定容器的类型。

(继承自 ControlBuilder)
ComplexPropertyEntries

获取复杂属性项的集合。

(继承自 ControlBuilder)
ControlType

获取要创建的控件的 Type

(继承自 ControlBuilder)
CurrentFilterResolutionService

获取一个 IFilterResolutionService 对象,在设计器中分析和保持控件时使用该对象管理与设备筛选器相关的服务。

(继承自 ControlBuilder)
DeclareType

获取代码生成将用于声明控件的类型。

(继承自 ControlBuilder)
FChildrenAsProperties

获取确定控件是否有设置到 trueParseChildrenAttributeChildrenAsProperties 的值。

(继承自 ControlBuilder)
FIsNonParserAccessor

获取确定控件是否实现了 IParserAccessor 界面的值。

(继承自 ControlBuilder)
HasAspCode

获取一个值,该值指示控件是否包含任何代码块。

(继承自 ControlBuilder)
ID

获取或设置要生成的控件的标识符属性。

(继承自 ControlBuilder)
InDesigner

返回 ControlBuilder 是否正在设计器中运行。

(继承自 ControlBuilder)
InPageTheme

获取一个布尔值,该值指示此 ControlBuilder 对象是否用于生成页主题。

(继承自 ControlBuilder)
ItemType

获取在绑定容器上设置的类型。

(继承自 ControlBuilder)
Localize

获取一个布尔值,该值指示由此 ControlBuilder 对象所创建的控件是否已本地化。

(继承自 ControlBuilder)
NamingContainerType

获取此生成器所创建控件的命名容器的类型。

(继承自 ControlBuilder)
PageVirtualPath

通过 ControlBuilder 实例获取要建立的页的虚拟路径。

(继承自 ControlBuilder)
Parser

获取负责分析控件的 TemplateParser

(继承自 ControlBuilder)
ServiceProvider

获取此 ControlBuilder 对象的服务对象。

(继承自 ControlBuilder)
SubBuilders

获取此 ControlBuilder 对象的 ControlBuilder 子对象的列表。

(继承自 ControlBuilder)
TagName

获取要生成的控件的标记名称。

(继承自 ControlBuilder)
TemplatePropertyEntries

获取模板属性项的集合。

(继承自 ControlBuilder)
ThemeResolutionService

获取一个 IThemeResolutionService 对象,该对象用于在设计时管理控件的主题和外观。

(继承自 ControlBuilder)

方法

AllowWhitespaceLiterals()

确定是处理还是忽略 HtmlSelect 控件中的空白。

AppendLiteralString(String)

将指定的文本内容添加到控件。 此方法由 ASP.NET 页面框架调用。

(继承自 ControlBuilder)
AppendSubBuilder(ControlBuilder)

将生成器添加到属于容器控件的任何子控件的 ControlBuilder 对象。

(继承自 ControlBuilder)
BuildObject()

生成此 ControlBuilder 对象所引用的控件的设计时实例。

(继承自 ControlBuilder)
CloseControl()

由分析器调用以通知生成器对控件的开始和结束标记的分析已完成。

(继承自 ControlBuilder)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetChildControlType(String, IDictionary)

获取 HtmlSelect 控件的子控件的 Type

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectPersistData()

创建此 ObjectPersistData 对象的 ControlBuilder 对象。

(继承自 ControlBuilder)
GetResourceKey()

检索此 ControlBuilder 对象的资源键。

(继承自 ControlBuilder)
GetType()

获取当前实例的 Type

(继承自 Object)
HasBody()

确定控件是否同时具有开始标记和结束标记。 此方法由 ASP.NET 页面框架调用。

(继承自 ControlBuilder)
HtmlDecodeLiterals()

确定 HTML 控件的字符串是否必须是 HTML 解码的。 此方法由 ASP.NET 页面框架调用。

(继承自 ControlBuilder)
Init(TemplateParser, ControlBuilder, Type, String, String, IDictionary)

实例化 ControlBuilder 后再进行初始化以便使用。 此方法由 ASP.NET 页面框架调用。

(继承自 ControlBuilder)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
NeedsTagInnerText()

确定控件生成器是否需要获取它的内部文本。 如果需要,则必须调用 SetTagInnerText(String) 方法。 此方法由 ASP.NET 页面框架调用。

(继承自 ControlBuilder)
OnAppendToParentBuilder(ControlBuilder)

通知 ControlBuilder 正在将它添加到父控件生成器。

(继承自 ControlBuilder)
ProcessGeneratedCode(CodeCompileUnit, CodeTypeDeclaration, CodeTypeDeclaration, CodeMemberMethod, CodeMemberMethod)

启用自定义控件生成器以访问生成的代码文档对象模型 (CodeDom),并在分析和生成控件的过程中插入和修改代码。

(继承自 ControlBuilder)
SetResourceKey(String)

设置此 ControlBuilder 对象的资源键。

(继承自 ControlBuilder)
SetServiceProvider(IServiceProvider)

设置此 ControlBuilder 对象的服务对象。

(继承自 ControlBuilder)
SetTagInnerText(String)

ControlBuilder 提供控件标记的内部文本。

(继承自 ControlBuilder)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅