zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    MongoDB 常用故障排查工具
    MongoDB ServerStatus返回信息
    SQL Server 2012实施与管理实战指南(笔记)——Ch6连接的建立和问题排查
    SQL Server 2012实施与管理实战指南(笔记)——Ch5启动SQL Server服务和数据库
    [20140504] ADO.NET客户端超时
    SQL Server 2012实施与管理实战指南(笔记)——Ch4数据库连接组件
    SQL Server 2012实施与管理实战指南(笔记)——Ch3Alwayson可用组
    SQL Server 2014新特性——Buffer Pool扩展
    SQL Server 2014新特性——事务持久性控制
    SQL Server 2014新特性——基数评估(白皮书阅读笔记)
  • 原文地址:https://www.cnblogs.com/bellin124/p/15189674.html
Copyright © 2011-2022 走看看