首页 > 代码库 > DropDownList控件的使用方法

DropDownList控件的使用方法

1. 使用代码添加数据

        <asp:DropDownList ID="DropDownList1" runat="server">        </asp:DropDownList>       //1.1使用代码添加数据        ListItem item = new ListItem(".net","1");        this.DropDownList1.Items.Add(item);        item = new ListItem("java", "2");        this.DropDownList1.Items.Add(item);        item = new ListItem("php", "3");        this.DropDownList1.Items.Add(item);        item = new ListItem("选择全部", "0");        this.DropDownList1.Items.Insert(0, item);//在第一个索引处添加‘选择全部’选项

2. 使用代码绑定数据源

        <asp:DropDownList ID="DropDownList2" runat="server">        </asp:DropDownList> //2.1使用代码绑定数据源        CategoriesManager manager = new CategoriesManager();        this.DropDownList2.DataSource=manager.GetDataTable();        this.DropDownList2.DataTextField = "name";        this.DropDownList2.DataValueField = "id";        this.DropDownList2.DataBind();        this.DropDownList2.Items.Insert(0,new ListItem("选择全部", "0"));

3. 使用数据绑定控件绑定数据源

        <asp:DropDownList ID="DropDownList3" runat="server"             DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id">        </asp:DropDownList>        <asp:SqlDataSource ID="SqlDataSource1" runat="server"             ConnectionString="<%$ ConnectionStrings:BookShopConnectionString %>"             SelectCommand="SELECT [Id], [Name] FROM [Categories]"></asp:SqlDataSource>

 

DropDownList控件的使用方法