ASP.Net/Asp Control/DropDownList

Материал из .Net Framework эксперт
Версия от 11:52, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add elements in an Array to asp:dropdownlist (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load()
    Dim AnimalArray(4) As String
    Dim strAnimal As String
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    Array.Reverse(AnimalArray)
    For Each strAnimal In AnimalArray
      MyDropDownList.Items.Add(strAnimal)
    Next
  End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Add ListItem to DropDownList

<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
    if(!IsPostBack) {
        ListItem item;
        item = new ListItem("C#", "1");
        languageDropDownList.Items.Add(item);
        item = new ListItem("VB.NET", "2");
        languageDropDownList.Items.Add(item);
        item = new ListItem("Java", "3");
        languageDropDownList.Items.Add(item);
        favoriteLanguage.Text = languageDropDownList.SelectedItem.Value;
    }
}
protected void DropDownListSelectionChanged(object o, EventArgs e) {
    favoriteLanguage.Text = languageDropDownList.SelectedItem.Value;
}
</script>
<form runat="server">
<asp:DropDownList 
    id="languageDropDownList"
    runat="server" 
    OnSelectedIndexChanged="DropDownListSelectionChanged" >
</asp:DropDownList><br/>
Favorite Language: <b><asp:label runat="server" id="favoriteLanguage" style="color:blue" /></b><br />
<asp:button runat="server" Text="Submit"/>
</form>



Add values to asp:DropDownList (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
      Array.Reverse(ColorList)
      
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & ddlColorList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
            <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Add value to asp:DropDownList dynamically (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    Dim FontList as new ArrayList()
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
    
      FontList.Add("Times New Roman")
      FontList.Add("Arial")
      FontList.Add("Verdana")
      FontList.Add("Comic Sans MS")
    
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
    
        ddlFontList.DataSource = FontList
        ddlFontList.DataBind()
    
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & _
        ddlColorList.SelectedItem.Value & " text written in " & _
        ddlFontList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
      lblOutputMessage.Font.Name = _
        ddlFontList.SelectedItem.Text
    
    End Sub
    
    Sub btnAddFont_Click(sender As Object, e As EventArgs)
      FontList.Add(txtAddFont.Text)
    
      ddlFontList.DataSource = FontList
      ddlFontList.DataBind()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
        </p>
        <p>
            Then select a font sytle from the list: 
            <asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
        </p>
        <p>
            &nbsp;<asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
        <p>
            Enter a new font for the list:<asp:TextBox id="txtAddFont" runat="server"></asp:TextBox>
            <asp:Button id="btnAddFont" onclick="btnAddFont_Click" runat="server" Text="Add New Font"></asp:Button>
        </p>
    </form>
</body>
</html>



Appending Data Items

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Append List Items</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList
        id="ddlProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        AppendDataBoundItems="True"
        Runat="server">
        <asp:ListItem
            Text="Select a Product"
            Value="" />
    </asp:DropDownList>
    <asp:RequiredFieldValidator
        id="valProducts"
        Text="(Required)"
        ControlToValidate="ddlProducts"
        Runat="server" />
    <asp:Button
        id="btnSubmit"
        Text="Submit Form"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Id, Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>



Change asp:dropdownlist fore and background color (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Working with the Appearance of a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<B>Select a Color:</B><BR>
<BR><BR>
<asp:dropdownlist
    id="ddl1"
    accesskey="1"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" Selected>Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
<BR><BR>
<asp:dropdownlist
    id="ddl2"
    font-name="Comic Sans MS"
    font-size="9pt"
    font-bold="True"
    backcolor="lightyellow"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="SR" Selected>Bright Orange Green with Red</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
<BR><BR>
<asp:dropdownlist
    id="ddl3"
    backcolor="darkred"
    forecolor="yellow"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" Selected>Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
</form>
</BODY>
</HTML>



Change background color for asp:dropdownlist (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub ddl1_Changed(Sender As Object, E As EventArgs)
    ddl1.BackColor = System.Drawing.Color.FromName(ddl1.SelectedItem.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when an Item is Selected in a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:dropdownlist id="ddl1" autopostback="True" onselectedindexchanged="ddl1_Changed" runat="server">
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" >Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
</asp:dropdownlist>
</form>
</BODY>
</HTML>



Create asp:dropdownlist dynamically (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim I as Integer
    Dim J as Integer
    For I = 1 to txtNumber.Text
        Dim MyDDL = New DropDownList
        MyDDL.ID = "ddlDynamic" & I
        For J = 1 to 3
            Dim MyLI as New ListItem
            MyLI.Text = "Control Number: " & I & "-" & J
            MyLI.Value = I & J
            MyDDL.Items.Add(MyLI)
        Next
        frmMyPage.Controls.Add(MyDDL)
        Dim MyLiteral = New LiteralControl
        MyLiteral.Text = "<BR><BR>"
        frmMyPage.Controls.Add(MyLiteral)
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a DropDownList Control in Code</TITLE>
</HEAD>
<Body LEFTMARGIN="60">
<form 
    id="frmMyPage"
    runat="server"
>
<B>Enter the number of DropDownList controls you want:</B><BR><BR>
<asp:TextBox 
    id="txtNumber" 
    runat=server 
/>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
<BR><BR>
</form>
</BODY>
</HTML>



Depending Dropdowns

<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>Depending Dropdowns</title>
  </HEAD>
  <script runat="server">
         Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             If Not IsPostBack Then
                 countryList.Items.Clear()
                 countryList.Items.Add(New ListItem("- Make a selection -"))
                 countryList.Items.Add(New ListItem("A", "B"))
                 countryList.Items.Add(New ListItem("B", "F"))
             End If
         End Sub
         Private Sub SelectedIndexChangedEventHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
             cityList.Items.Clear()
             If countryList.SelectedItem.Value = "B" Then
     
                 cityList.Items.Add(New ListItem("A1"))
                 cityList.Items.Add(New ListItem("A2"))
                 cityList.Items.Add(New ListItem("A3"))
             ElseIf countryList.SelectedItem.Value = "F" Then
                 cityList.Items.Add(New ListItem("B1"))
                 cityList.Items.Add(New ListItem("B2"))
                 cityList.Items.Add(New ListItem("B3"))
             End If
     
         End Sub
  </script>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:DropDownList id="countryList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="SelectedIndexChangedEventHandler"></asp:DropDownList>
      <asp:DropDownList id="cityList" runat="server"></asp:DropDownList>
    </form>
  </body>
</HTML>



Dropdownlist Binded to ArrayList

<%@ Page Language="VB" %>
<html>
<head>
   <title>Collection DataBinding Example</title>
   <script runat="server">
      Dim FontColor As String
      Sub Page_Load()
         If Not IsPostBack Then
            Dim Colors As New ArrayList()
            Colors.Add("Red")
            Colors.Add("Green")
            Colors.Add("Blue")
            Color.DataSource = Colors
            DataBind()
            Color.SelectedIndex = 0
         End If
         Output.Text = "The value for FontColor is " & _
            Color.SelectedItem.Value & "."
         Output.ForeColor = _
            System.Drawing.Color.FromName(Color.SelectedItem.Value)
         End Sub
  </script>
</head>
<body>
   <h1>Collection DataBinding Example</h1>
   <form runat="server">
      Choose a color from the list for the font color:
      <asp:dropdownlist id="Color" autopostback="True" runat="server"/>
      <br/>
      <asp:label id="Output" runat="server"/>
   </form>
</body>
</html>



For loop controlled by dropdownlist (C#)

<script language="C#" runat="server">
void Page_Load()
{ 
  int number, counter;
  if (Page.IsPostBack) {  
    number = Convert.ToInt32(NumberAttendees.SelectedItem.Value);
    Message1.Text = "";
    for (counter = 0; counter < number; counter ++)
      {
        Message1.Text += "Attendee Name ___________________<br /><br />" + 
              "Attendee Age _________________<br /><br /><hr /><br />";
      }
  }
}
</script>
<html>
<head>
<title>Loop Example</title>
</head>
<body>
<form runat="server">
Enter the number of attendees(max 6):
<br>
<br>
<asp:dropdownlist id="NumberAttendees" runat="server">
  <asp:listitem>1</asp:listitem>
  <asp:listitem>2</asp:listitem>
  <asp:listitem>3</asp:listitem>
  <asp:listitem>4</asp:listitem>
  <asp:listitem>5</asp:listitem>
  <asp:listitem>6</asp:listitem>
</asp:dropdownlist>
<br>
<br>
<input type="submit">
<br>
<br>
<asp:label id="Message1" runat="server"/>
</form>
</body>
</html>



Get form data: asp:checkbox, asp:DropDownList, asp:TextBox (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
      Dim Message As String
    
      Message = "Hello " & txtName.Text & "."
    
      If chkTour.Checked Then
        Message &= cboCountry.SelectedItem.Text
      End If
    
      If radMail.SelectedItem.Value = "HTML" Then
        Label1.Text = "Using HTML format for mail. Sending:<p />" & Message
      Else
        Label1.Text = "Using plain format for mail. Sending:<p />" & Message
      End If
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Subscribe to our mailing list: 
        </p>
        <p>
            <table style="WIDTH: 300px; HEIGHT: 150px">
                <tbody>
                    <tr>
                        <td>
                            Name:</td>
                        <td>
                            <asp:TextBox id="txtName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            E-mail Address:</td>
                        <td>
                            <asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Country:</td>
                        <td>
                            <asp:DropDownList id="cboCountry" runat="server">
                                <asp:ListItem Value="US" Selected="True">United States</asp:ListItem>
                                <asp:ListItem Value="UK">United Kingdom</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mail me tour details:</td>
                        <td>
                            <asp:CheckBox id="chkTour" runat="server"></asp:CheckBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mail format:</td>
                        <td>
                            <asp:RadioButtonList id="radMail" runat="server">
                                <asp:ListItem Value="HTML">HTML</asp:ListItem>
                                <asp:ListItem Value="Text">Text</asp:ListItem>
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:ImageButton id="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="http://www.nfex.ru/style/logo.png"></asp:ImageButton>
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
    </form>
    <p>
        <asp:Label id="Label1" runat="server"></asp:Label>
    </p>
</body>
</html>



Get selected item from asp:dropdownlist (C#)

<script runat="server" language="C#">
  void Page_Load()
    {
      if (Page.IsPostBack) {   
          Message.Text = "You have selected " + list1.SelectedItem.Value;
      }
    }
</script>
<html>
  <head>
    <title>Drop Down List Example</title>
  </head>
  <body>
    <asp:label id="Message" runat="server"/> 
    <br />
    <form runat="server">
    Which city do you wish to look at hotels for?<br /><br />
    <asp:dropdownlist id="list1" runat="server">
      <asp:listitem>Madrid</asp:listitem>
      <asp:listitem>Oslo</asp:listitem>
      <asp:listitem>Lisbon</asp:listitem>
    </asp:dropdownlist>
    <br /><br /><br /><br />
    <input type="Submit">
    </form>
  </body>
</html>



On selected state changed in asp:dropdownlist (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub ddl1_Changed(Sender As Object, E As EventArgs)
    ddl1.BackColor = System.Drawing.Color.FromName(ddl1.SelectedItem.Text)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when an Item is Selected in a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:dropdownlist id="ddl1" autopostback="True" onselectedindexchanged="ddl1_Changed" runat="server">
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" >Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
</asp:dropdownlist>
</form>
</BODY>
</HTML>



Selected Index Changed event for asp:DropDownList (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
      Select Case DropDownList1.SelectedItem.Value
        Case 0: Label1.Text = String.Empty  " Reset the Label1.Text
        Case 1: Label1.Text = "You got Grade E!"
        Case 2: Label1.Text = "You got Grade D!"
        Case 3: Label1.Text = "You got Grade C!"
        Case 4: Label1.Text = "You got Grade B!"
        Case 5: Label1.Text = "You got Grade A!"
      End Select
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="0" Selected="True">- Please select a range of score -</asp:ListItem>
                <asp:ListItem Value="1">0 - 20</asp:ListItem>
                <asp:ListItem Value="2">21 - 40</asp:ListItem>
                <asp:ListItem Value="3">40 - 60</asp:ListItem>
                <asp:ListItem Value="4">61 - 80</asp:ListItem>
                <asp:ListItem Value="5">81 - 100</asp:ListItem>
            </asp:DropDownList>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Select the value of asp:DropDownList Selected Item (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
      Select Case DropDownList1.SelectedItem.Value
        Case 0: Label1.Text = String.Empty  " Reset the Label1.Text
        Case 1: Label1.Text = "You got Grade E!"
        Case 2: Label1.Text = "You got Grade D!"
        Case 3: Label1.Text = "You got Grade C!"
        Case 4: Label1.Text = "You got Grade B!"
        Case 5: Label1.Text = "You got Grade A!"
      End Select
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="0" Selected="True">- Please select a range of score -</asp:ListItem>
                <asp:ListItem Value="1">0 - 20</asp:ListItem>
                <asp:ListItem Value="2">21 - 40</asp:ListItem>
                <asp:ListItem Value="3">40 - 60</asp:ListItem>
                <asp:ListItem Value="4">61 - 80</asp:ListItem>
                <asp:ListItem Value="5">81 - 100</asp:ListItem>
            </asp:DropDownList>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Set asp:DropDownList selected index (VB.net)

<%@ Page Language="vb" %>
<html>
<head>
   <title>Selection Control Example</title>
   <script runat="server">
      Sub Page_Load()
         MyCheckBox1.Checked = True
         MyRadioButton1.Checked = False
         MyListBox.SelectionMode = ListSelectionMode.Multiple
         MyDropDownList.SelectedIndex = 1
         MyCheckBoxList.RepeatDirection = RepeatDirection.Horizontal
         MyRadioButtonList.RepeatLayout = RepeatLayout.Table
      End Sub
   </script>
</head>
<body>
   <h1>Selection Control Example</h1>
   <form runat="server">
      <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               CheckBox Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:checkbox id="MyCheckBox1" 
                  text="Vanilla" runat="server" />
               <asp:checkbox id="MyCheckBox2" 
                  text="Chocolate" runat="server" />
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               RadioButton Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:radiobutton id="MyRadioButton1" groupname="Group1" 
                  checked=True text="Yes" runat="Server"/>
               <asp:radiobutton id="MyRadioButton2" groupname="Group1"
                  text="No" runat="Server"/>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               ListBox Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:listbox id="MyListBox" runat="server">
                  <asp:listitem value="Vanilla" selected="true">Vanilla</asp:listitem>
                  <asp:listitem value="Chocolate">Chocolate</asp:listitem>
                  <asp:listitem value="Strawberry">Strawberry</asp:listitem>
               </asp:listbox>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               DropDownList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:dropdownlist id="MyDropDownList" runat="server">
                  <asp:listitem value="Single" selected="true">Single</asp:listitem>
                  <asp:listitem value="Multiline">Multiline</asp:listitem>
                  <asp:listitem value="Password">Password</asp:listitem>
               </asp:dropdownlist>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               CheckBoxList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:checkboxlist id="MyCheckBoxList"
                  repeatdirection="vertical" runat="server">
                  <asp:listitem value="Vanilla" text="Vanilla"/>
                  <asp:listitem value="Chocolate" text="Chocolate"/>
                  <asp:listitem value="Strawberry" text="Strawberry"/>
               </asp:checkboxlist>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               RadioButtonList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:radiobuttonlist id="MyRadioButtonList" repeatdirection="Horizontal" runat="server">
                  <asp:listitem value="Female" text="Female" selected="true"/>
                  <asp:listitem value="Male" text="Male"/>
               </asp:radiobuttonlist>
            </asp:tablecell>
         </asp:tablerow>
      </asp:table>
   </form>
</body>
</html>



Set asp:dropdownlist value statically (VB.net)

<script runat="server" language="vb">
  Sub Page_Load()
    if Page.IsPostback then
      Message.Text = "You have selected " + list1.SelectedItem.Value
    end if
  End Sub
</script>
<html>
  <head>
    <title>Drop Down List Example</title>
  </head>
  <body>
    <asp:label id="Message" runat="server"/> 
    <br />
    <form runat="server">
    Which city do you wish to look at hotels for?<br /><br />
    <asp:dropdownlist id="list1" runat="server">
      <asp:listitem>Madrid</asp:listitem>
      <asp:listitem>Oslo</asp:listitem>
      <asp:listitem>Lisbon</asp:listitem>
    </asp:dropdownlist>
    <br /><br /><br /><br />
    <input type="Submit">
    </form>
  </body>
</html>



Set font name, size and style for asp:dropdownlist (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Working with the Appearance of a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<B>Select a Color:</B><BR>
<BR><BR>
<asp:dropdownlist
    id="ddl1"
    accesskey="1"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" Selected>Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
<BR><BR>
<asp:dropdownlist
    id="ddl2"
    font-name="Comic Sans MS"
    font-size="9pt"
    font-bold="True"
    backcolor="lightyellow"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="SR" Selected>Bright Orange Green with Red</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
<BR><BR>
<asp:dropdownlist
    id="ddl3"
    backcolor="darkred"
    forecolor="yellow"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" Selected>Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
    <asp:listitem value="Go" text="Gold"/>
</asp:dropdownlist>
</form>
</BODY>
</HTML>



Set selected Item in asp:DropDownList (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        ddl1.SelectedIndex = 2
    End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    lblDataSelected.Text = "Selected Text: " _
        & ddl1.SelectedItem.Text & "<BR>Selected Value: " _
        & ddl1.SelectedItem.Value & "<BR>Selected Index: " _
        & ddl1.SelectedIndex       
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Reading and Writing the Selected Item in a DropDownList Control</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:label 
    id="lblDataSelected" 
    runat="server"
/>
<BR><BR>
<asp:dropdownlist
    id="ddl1"
    accesskey="1"
    runat="server"
>
    <asp:listitem value="Bu">Blue</asp:listitem>
    <asp:listitem value="Re">Red</asp:listitem>
    <asp:listitem value="Gr">Green</asp:listitem>
    <asp:listitem value="Pu" >Purple</asp:listitem>
    <asp:listitem value="Ba">Black</asp:listitem>
</asp:dropdownlist>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>



Sort array elements and add them to the asp:dropdownlist (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load()
    Dim AnimalArray(4) As String
    Dim strAnimal As String
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    Array.Sort(AnimalArray)
    For Each strAnimal In AnimalArray
      MyDropDownList.Items.Add(strAnimal)
    Next
  End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Use ArrayList to fill dropdown value (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
         ArrayList list = new ArrayList();
         list.Add("none");
         list.Add("Canada Post");
         list.Add("UPS");
    
         list.Insert(1,"FedEx");
    
         MyDropDownList.DataSource = list;
         MyDropDownList.DataBind();
    }
</script>
<html>
<head>
    <title>ArrayList Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <asp:dropdownlist id="MyDropDownList" runat="server"></asp:dropdownlist>
    </form>
</body>
</html>



Working with the DropDownList Control and asp:SqlDataSource

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblProduct.Text = ddlProducts.SelectedItem.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show DropDownList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList
        id="ddlProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        Runat="server" />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <hr />
    <asp:Label
        id="lblProduct"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Id, Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>