ASP.Net 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);//在第一个索引处添加‘选择全部’选项

自己的代码:

 <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>

 protected void Page_Load(object sender, EventArgs e)
        {
            //数据库信息查询
       DB db
= new DB(); string sql = "select eqpid from equn@odsprd where eqptype='WIO' and eqpid<>'AWAC01'"; DataTable datatable =db.getdatatable(sql); //下拉选择 将获取的datatable数据添加到DropDownList ListItem item = null; /*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);//在第一个索引处添加‘选择全部’选项 */ for (int i = 0; i < datatable.Rows.Count; i++) { //Response.Write(datatable.Rows[i][0].ToString() ); item = new ListItem(datatable.Rows[i][0].ToString(), i.ToString()); this.DropDownList1.Items.Add(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"));

自己的代码:

 <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
        {
            DB db = new DB();
            string sql = "select eqpid  from equn@odsprd where eqptype='WIO' and eqpid<>'AWAC01'";
            DataTable datatable =db.getdatatable(sql);
            //下拉选择 将获取的datatable数据绑定到DropDownList 绑定数据方法DataBind()
          
            this.DropDownList2.DataSource = datatable;
            this.DropDownList2.DataTextField = "eqpid";
            //this.DropDownList2.DataValueField = "id";//没有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>

自己未做验证,留下笔记以备后用

转载地址:http://t.zoukankan.com/xyyt-p-3979097.html

 

posted @ 2021-08-26 15:04  bellin124  阅读(1024)  评论(0编辑  收藏  举报