Syntaxe déclarative du contrôle serveur HtmlSelect

Crée un contrôle côté serveur mappé à l'élément HTML <select> et permet de créer un contrôle de liste.

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

Notes

Utilisez le contrôle HtmlSelect pour programmer en vous servant de l'élément <select> HTML. Par défaut, ce contrôle est rendu sous la forme d'une zone de liste déroulante modifiable. Toutefois, si vous autorisez les sélections multiples (en spécifiant l'attribut Multiple) ou si vous spécifiez une valeur supérieure à 1 pour la propriété Size, le contrôle est affiché comme une zone de liste.

Vous pouvez également lier le contrôle à une source de données. Définissez la propriété DataSource pour spécifier la source de données à lier au contrôle. Une fois la source de données liée au contrôle, vous pouvez spécifier le champ à lier aux propriétés Value et Text en définissant respectivement les propriétés DataValueField et DataTextField.

Exemple

L'exemple suivant utilise des entrées d'un contrôle HtmlSelect pour définir la couleur d'arrière-plan d'un contrôle span. Il montre également comment utiliser la propriété Items pour ajouter de nouveaux éléments d'option à la liste Select. Cette propriété est du type ListItemCollection et peut donc accéder à la méthode Add de cette classe. L'exemple de code réalise cette action dans le gestionnaire d'événements 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>

L'exemple suivant montre comment lier un contrôle HtmlSelect à ArrayList, déclaré dans l'événement Page_Load. Il existe également un événement SubmitBtn_Click qui affiche la valeur liée aux données sélectionnée lorsque l'utilisateur clique sur un contrôle HtmlInputButton dans la page rendue.

La propriété Id du contrôle Select est StateSelect et la propriété DataSource du contrôle a les valeurs créées par ArrayList lors du chargement de la page. Ensuite, la méthode DataBind du contrôle Select lie les valeurs de ArrayList au contrôle lui-même.

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

Voir aussi

Référence

HtmlSelect

Syntaxe déclarative du contrôle serveur HtmlForm

System.Web.UI.HtmlControls

Autres ressources

Contrôles serveur HTML