HtmlSelect 服务器控件声明性语法
[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]
创建一个服务器端控件,该控件映射到 <select> HTML 元素并允许您创建列表控件。
<select
DataSourceID="string"
DataTextField="string"
EnableViewState="False|True"
Id="string"
Visible="False|True"
OnDataBinding="OnDataBinding event handler"
OnDisposed="OnDisposed event handler"
OnInit="OnInit event handler"
OnLoad="OnLoad event handler"
OnPreRender="OnPreRender event handler"
OnServerChange="OnServerChange event handler"
OnUnload="OnUnload event handler"
runat="server"
>
<option>value1</option>
<option>value2</option>
</select>
备注
使用 HtmlSelect 控件可以对 HTML <select> 元素进行编程。 默认情况下,此控件呈现为下拉列表框。 但是,如果允许多重选择(通过指定 Multiple 特性)或为 Size 属性指定大于 1 的值,则该控件将显示为列表框。
还可以将该控件绑定到数据源。 设置 DataSource 属性以指定要将其绑定到该控件的数据源。 在将数据源绑定到该控件后,可以通过设置 DataValueField 和 DataTextField 属性,分别指定将哪个字段绑定到 Value 和 Text 属性。
示例
下面的示例使用 HtmlSelect 控件中的项为 span 控件设置背景色。 该示例还显示如何使用 Items 属性将新的选项添加到选择列表中。 此属性属于 ListItemCollection 类型,因此可以访问该类的 Add 方法。 此代码示例通过 AddToList_Click 事件处理程序实现该操作。
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>HtmlSelect Control</title>
<script runat="server">
Sub Apply_Click(Source As Object, e As EventArgs)
Span1.Style("background-color") = ColorSelect.Value
End Sub
Sub AddToList_Click(Source As Object, e As EventArgs)
ColorSelect.Items.Add(Text1.Value)
End Sub
</script>
</head>
<body>
<h3>HtmlSelect Sample</h3>
<form id="Form1" runat="server">
Select a color:<br />
<select id="ColorSelect" runat="server">
<option>SkyBlue</option>
<option>LightGreen</option>
<option>Gainsboro</option>
<option>LemonChiffon</option>
</select>
<input id="Button1" type="button" runat="server"
value="Apply" onserverclick="Apply_Click" />
<p />
Don't see your color in the list above? You can add it here:<br />
<input type="text" id="Text1" runat="server" />
<input id="Button2" type="button" runat="server"
value="Add to List" onserverclick="AddToList_Click" />
<p />
<span id="Span1" runat="server">
Click the button to apply a background color to this span.
</span>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>HtmlSelect Control</title>
<script runat="server">
void Apply_Click(object Source, EventArgs e)
{
Span1.Style["background-color"] = ColorSelect.Value;
}
void AddToList_Click(object Source, EventArgs e)
{
ColorSelect.Items.Add(Text1.Value);
}
</script>
</head>
<body>
<h3>HtmlSelect Sample</h3>
<form id="Form1" runat="server">
Select a color:<br />
<select id="ColorSelect" runat="server">
<option>SkyBlue</option>
<option>LightGreen</option>
<option>Gainsboro</option>
<option>LemonChiffon</option>
</select>
<input id="Button1" type="button" runat="server"
value="Apply" onserverclick="Apply_Click" />
<p />
Don't see your color in the list above? You can add it here:<br />
<input type="text" id="Text1" runat="server" />
<input id="Button2" type="button" runat="server"
value="Add to List" onserverclick="AddToList_Click" />
<p />
<span id="Span1" runat="server">
Click the button to apply a background color to this span.
</span>
</form>
</body>
</html>
下面的示例演示如何将 HtmlSelect 控件绑定到在 Page_Load 事件中声明的 ArrayList。 当用户在呈现页上单击 HtmlInputButton 控件时,也会出现 SubmitBtn_Click 事件,它显示选定的数据绑定值。
选定控件的 Id 属性为 StateSelect,并且该控件的 DataSource 属性设置为加载页时 ArrayList 所创建的值。 然后,选定控件的 DataBind 方法将 ArrayList 中的值绑定到该控件本身。
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>HtmlSelect Control</title>
<script runat="server">
Sub Page_Load(Sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values As New ArrayList()
values.Add("IN")
values.Add("KS")
values.Add("MD")
values.Add("MI")
values.Add("OR")
values.Add("TN")
StateSelect.DataSource = values
StateSelect.DataBind()
End If
End Sub
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Span1.InnerHtml = "You chose: " & StateSelect.Value
End Sub
</script>
</head>
<body>
<h3>Data Binding to an HtmlSelect Control</h3>
<form id="Form1" runat="server">
Select a state:<br />
<select id="StateSelect" runat="server" />
<input id="Submit1" type="submit" value="Display Selected State"
onserverclick="SubmitBtn_Click" runat="server" />
<p />
<span id="Span1" runat="server" />
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>HtmlSelect Control</title>
<script runat="server">
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();
values.Add ("IN");
values.Add ("KS");
values.Add ("MD");
values.Add ("MI");
values.Add ("OR");
values.Add ("TN");
StateSelect.DataSource = values;
StateSelect.DataBind();
}
}
void SubmitBtn_Click(Object sender, EventArgs e)
{
Span1.InnerHtml = "You chose: " + StateSelect.Value;
}
</script>
</head>
<body>
<h3>Data Binding to an HtmlSelect Control</h3>
<form id="Form1" runat="server">
Select a state:<br />
<select id="StateSelect" runat="server" />
<input id="Submit1" type="submit" value="Display Selected State"
onserverclick="SubmitBtn_Click" runat="server" />
<p />
<span id="Span1" runat="server" />
</form>
</body>
</html>