第一种少量自定义数据时:

.aspx中的代码:

<asp:DropDownList ID="DropDownList1" runat="server">
                   <asp:ListItem Value="2">男</asp:ListItem>
                   <asp:ListItem Selected="True" Value="1">女</asp:ListItem>
               </asp:DropDownList>



.cs中的代码: 注意后台取值有取文字和编号两种


this.DropDownList1.SelectedItem.Text   //取文字 既男女
  this.DropDownList1.SelectedValue.ToString()  //取编号



第二种从数据库中取数据进行绑定:


DataSet dc= new DataSet();  

            sqlStr = "select book_class_id,book_class_name from book_class";


            dc = CC.GetDataSet(sqlStr, "00");   //取出数据放入表中,这里的CC是我自己的一个公共类 ,反正这里是取出数据放到一个表中就行了,后面给出常用公共类的代码
            this.DropDownList1.DataSource = dc.Tables[0];
            this.DropDownList1.DataTextField = "book_class_name"; //绑定文字对应的字段
            this.DropDownList1.DataValueField = "book_class_id";       //绑定编号对应的字段
            this.DropDownList1.DataBind();

后台.cs中取数据的方法同第一种方法



特殊:给DropDownList设默认值

  

this.DropDownList1.SelectedValue = 3;   // 3是编号
       this.DropDownList1.Items.FindByText(“计算机”).Selected = true;  //计算机是文字
 this.DropDownList1.Items.FindByValue("3").Selected = true; // 3是编号




添加默认选项

this.DropDownList1.Items.Insert(0, new ListItem("---选择类型---", "0"));
            this.DropDownList1.SelectedIndex = 0;








点击查看Web开发常用方法公共类