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

  • 相关阅读:
    深入字节码 -- 计算方法执行时间
    RubyCritic:一款不错的检测代码质量工具
    云告警平台 OneAlert :如何帮助运维工程师做好汇报?
    企业应用程序安全的新「守护神」
    另辟蹊径:云计算给企业带来的4个好处
    年度十佳 DevOps 博客文章(后篇)
    自定义 Lint 规则简介
    静态分析安全测试(SAST)优缺点探析
    css添加了原始滚动条要隐藏滚动条的显示
    jquery实现图片切换
  • 原文地址:https://www.cnblogs.com/bellin124/p/15189674.html
Copyright © 2011-2022 走看看