Share via


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 控制項中的項目 (Item),請將每個項目 (Entry) 的 ListItem 項目 (Element) 置於 ListBox 控制項的開頭和結尾標記之間。

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 伺服器控制項語法