Partager via


Syntaxe déclarative du contrôle serveur Web ListBox

Mise à jour : novembre 2007

Crée une zone de liste à sélection simple ou sélection multiple.

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

Notes

Utilisez le contrôle ListBox pour créer un contrôle de liste qui permet la sélection d'un seul ou de plusieurs éléments. Utilisez la propriété Rows pour spécifier la hauteur du contrôle. Pour permettre la sélection de plusieurs éléments, affectez Multiple à la propriété SelectionMode.

Pour spécifier les éléments que vous voulez voir apparaître dans le contrôle ListBox, insérez un élément ListItem pour chaque entrée entre les balises d'ouverture et de fermeture du contrôle ListBox.

Le contrôle ListBox prend également en charge la liaison de données. Pour lier le contrôle à une source de données, commencez par créer une source de données, par exemple l'un des objets DataSourceControl, qui contient les éléments à afficher dans le contrôle. Ensuite, utilisez la méthode DataBind pour lier la source de données au contrôle ListBox. Utilisez les propriétés DataTextField et DataValueField pour spécifier le champ de la source de données à lier respectivement aux propriétés Text et Value de chaque élément de liste du contrôle. Le contrôle ListBox affichera désormais les informations à partir de la source de données.

Si la propriété SelectionMode a la valeur Multiple, déterminez les éléments sélectionnés dans le contrôle ListBox en exécutant une itération sur la collection Items et en testant la propriété Selected de chaque élément de la collection. Si la propriété SelectionMode a la valeur Single, vous pouvez utiliser la propriété SelectedIndex pour déterminer l'index de l'élément sélectionné. L'index peut ensuite être utilisé pour récupérer l'élément dans la collection Items.

Pour plus d'informations sur les propriétés et événements du contrôle serveur Web ListBox, consultez la documentation de la classe ListBox.

Exemple

Le code suivant montre comment utiliser le contrôle ListBox pour afficher à l'utilisateur une liste d'options prédéfinies. L'élément choisi par l'utilisateur est affiché dans un contrôle 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>

Voir aussi

Référence

ListBox

Autres ressources

Syntaxe des contrôles serveur Web