下面的示例说明如何通过数据绑定创建 DropDownList 控件。[Visual Basic] 
<html>
<head>
   <script language="VB" runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            Dim myList As New ArrayList()
            myList.Add("Item 1")
            myList.Add("Item 2")
            myList.Add("Item 3")
            myList.Add("Item 4")
            myList.Add("Item 5")
            DropDownList1.DataSource = myList
            DropDownList1.DataBind()
        End If
    End Sub 'Page_Load
    Sub Button_Click(sender As Object, e As EventArgs)
        Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
    End Sub 'Button_Click
  </script>
</head>
 
<body>
   <form runat="server">
      <h3>DropDownList Data Bind Example</h3>
      Select an item from the list and click the submit button.
      <p>
      <asp:DropDownList id="DropDownList1" 
           runat="server"/>
      <br><br>
      <asp:Button id="Button1" 
           Text="Submit" 
           OnClick="Button_Click" 
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>
[C#] 
<html>
<head>
   <script language="C#" runat="server">
      void Page_Load(Object sender, EventArgs e)
      {
         if(!IsPostBack)
         {
            ArrayList myList = new ArrayList();
            myList.Add ("Item 1");
            myList.Add ("Item 2");
            myList.Add ("Item 3");
            myList.Add ("Item 4");
            myList.Add ("Item 5");
            DropDownList1.DataSource = myList;
            DropDownList1.DataBind();
         }
      }
       
      void Button_Click(Object sender, EventArgs e) 
      {
         Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";  
      }
   </script>
</head><body>
   <form runat="server">
      <h3>DropDownList Data Bind Example</h3>
      Select an item from the list and click the submit button.
      <p>
      <asp:DropDownList id="DropDownList1" 
           runat="server"/>
      <br><br>
      <asp:Button id="Button1" 
           Text="Submit" 
           OnClick="Button_Click" 
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>
[JScript] 
<html>
<head>
   <script language="JScript" runat="server">
      function Page_Load(sender : Object, e : EventArgs)
      {
         if(!IsPostBack)
         {
            var myList : ArrayList = new ArrayList();
            myList.Add ("Item 1");
            myList.Add ("Item 2");
            myList.Add ("Item 3");
            myList.Add ("Item 4");
            myList.Add ("Item 5");
            DropDownList1.DataSource = myList;
            DropDownList1.DataBind();
         }
      }
       
      function Button_Click(sender : Object, e : EventArgs) 
      {
         Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";  
      }
   </script>
</head>
 
<body>
   <form runat="server">
      <h3>DropDownList Data Bind Example</h3>
      Select an item from the list and click the submit button.
      <p>
      <asp:DropDownList id="DropDownList1" 
           runat="server"/>
      <br><br>
      <asp:Button id="Button1" 
           Text="Submit" 
           OnClick="Button_Click" 
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>

解决方案 »

  1.   

    用数据库的不同就是数据源不同
    DropDownList1.DataSource = myList;//myList可以是datareader或者DataTable...
      

  2.   

    简单的:
    <script runat="server">
         void Page_Load(Object sender,EventArgs e){
              ArrayList al = new ArrayList();
              al.Add( "select1" );
              al.Add( "select2" );
              al.Add( "select3" );
              al.Add( "select4" );
              DropDownList1.DataSource = al ;
              DropDownList1.DataBind();
         }
    </script><html>
        <head>
        </head>
        <body>
            <asp:DropDownList id="DropDownList1" runat="server" />
        </body>
    </html>
      

  3.   

    一下是一个自定义的 Dropdownlist绑定,调用时ddl换为Dropdownlist名,item_name换为要绑定的表中字段名,table_name换为表名
    Sub Dropdownlist_bind(ByVal ddl As DropDownList, ByVal item_name As String, ByVal table_name As String)
            Dim cmdSelectWhId As SqlCommand
            Dim dtrWhInfo As SqlDataReader
            Dim conManInfo As SqlConnection
            conManInfo = New SqlConnection("server=localhost;uid=sa;pwd=sa;database=wuliu")//连接数据库根据实际适当改动
            conManInfo.Open()
            cmdSelectWhId = New SqlCommand("select " + item_name + " from " + table_name, conManInfo)
            dtrWhInfo = cmdSelectWhId.ExecuteReader
            ddl.DataSource = dtrWhInfo
            ddl.DataTextField = item_name
            ddl.DataBind()
            conManInfo.Close()
        End Sub
      

  4.   

    将comy(泥娃)的 VB 程序中的Page_Load改成下面的代码就能实现
    将数据库(pubs)中表(Authors)的列(au_lanme)值绑定到dropdownlist上
    Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then        Dim conpubs As SqlConnection
            Dim cmdselect As SqlCommand
            Dim dtrresults As SqlDataReader
            Dim intfield As Integer        conpubs = New SqlConnection("Server=yinfan;uid=sa;pwd=sa;database=pubs")
            conpubs.Open()
            cmdselect = New SqlCommand("select au_lname from Authors", conpubs)
            dtrresults = cmdselect.ExecuteReader()
            
            DropDownList1.source=dtrresults
            DropDownList1.datatextField="au_lname"
            DropDownList1.DataBind()        dtrresults.close()
            conpubs.close()
      End If
     End Sub 
      

  5.   

    If IsPostBack = False Then
                Dim NComm1 As OleDbDataAdapter = New OleDbDataAdapter(str, objconn)
                objconn.Open()
                Dim DS1 As DataSet = New DataSet()            
                NComm1.Fill(DS1, "AllTable1")
                AllTables.DataSource = DS1.Tables("AllTable1").DefaultView//AllTables是一个dropdownlis的id
                AllTables.DataTextField = "tablenamec"
                AllTables.DataValueField = "tablenamee"
                AllTables.DataBind()
                objconn.Close()
                AllTables.Items.Add("")
                AllTables.SelectedIndex = AllTables.Items.Count - 1
                ...
            End If