ListBox Web 服务器控件声明性语法

更新:2007 年 11 月

创建单选或多选列表框。

<asp:ListBox
    AccessKey="string"
    AppendDataBoundItems="True|False"
    AutoPostBack="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    CssClass="string"
    DataMember="string"
    DataSource="string"
    DataSourceID="string"
    DataTextField="string"
    DataTextFormatString="string"
    DataValueField="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDataBound="DataBound event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedIndexChanged="SelectedIndexChanged event handler"
    OnTextChanged="TextChanged event handler"
    OnUnload="Unload event handler"
    Rows="integer"
    runat="server"
    SelectedIndex="integer"
    SelectedValue="string"
    SelectionMode="Single|Multiple"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
>
            <asp:ListItem
                Enabled="True|False"
                Selected="True|False"
                Text="string"
                Value="string"
            />
</asp:ListBox>

备注

使用 ListBox 控件创建允许进行单项或多项选择的列表控件。使用 Rows 属性指定控件的高度。若要启用多项选择,请将 SelectionMode 属性设置为 Multiple

若要指定要在 ListBox 控件中显示的项,请针对每项在 ListBox 控件的开始标记和结束标记之间放置一个 ListItem 元素。

ListBox 控件还支持数据绑定。若要将控件绑定到数据源,请先创建一个数据源,例如一个 DataSourceControl 对象,它包含要在控件中显示的项。下一步,使用 DataBind 方法将数据源绑定到 ListBox 控件。使用 DataTextFieldDataValueField 属性分别指定将数据源中哪个字段绑定到控件中每个列表项的 Text 和 Value 属性。现在,ListBox 控件将显示数据源中的信息。

如果 SelectionMode 属性设置为 Multiple,请通过循环访问 Items 集合以及测试该集合中每个项的 Selected 属性,来确定 ListBox 控件中的选定项。如果 SelectionMode 属性设置为 Single,则可使用 SelectedIndex 属性来确定选定项的索引。然后,可以使用该索引从 Items 集合中检索项。

有关 ListBox Web 服务器控件的属性和事件的详细信息,请参见 ListBox 类文档。

示例

下面的示例演示如何使用 ListBox 控件向用户显示预定义选项列表。用户选择的项显示在 Label 控件中。

<%@ 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>ListBox Example</title>
<script language="VB" runat="server">

    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
        If ListBox1.SelectedIndex > - 1 Then
            Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
        End If
    End Sub 'SubmitBtn_Click

  </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form id="form1" runat="server">

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
         <asp:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>

      </asp:ListBox>

      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />

      <asp:Label id="Label1" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           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>ListBox Example</title>
<script language="C#" runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         if (ListBox1.SelectedIndex > -1)
            Label1.Text="You chose: " + ListBox1.SelectedItem.Text;
      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>

   <form id="form1" runat="server">

      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
         <asp:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>

      </asp:ListBox>

      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />

      <asp:Label id="Label1" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

   </form>

</body>
</html>

请参见

参考

ListBox

其他资源

Web 服务器控件语法