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

  • 相关阅读:
    linux crontab 定时使用方法
    crontab 选择编辑器 select-editor
    设置定时任务为每天凌晨2点执行和每小时执行一次
    性能测试工具--SIEGE安装及使用简介 siege压力测试
    Vue基础
    使用 supervisor 管理进程
    长按listview弹出选项列表对话框
    左右滑动弹窗之间短信内容区域来显示上一条和下一条短信
    在开机广播中启动服务
    Android spinner 样式及其使用详解
  • 原文地址:https://www.cnblogs.com/bellin124/p/15189674.html
Copyright © 2011-2022 走看看