zoukankan      html  css  js  c++  java
  • C# DropDownList绑定添加新数据的三种方法

    一、在前台手动绑定

    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="1">南京</asp:ListItem>
        <asp:ListItem Value="2">扬州</asp:ListItem>
        <asp:ListItem Value="3">徐州</asp:ListItem>
        <asp:ListItem Value="4">苏州</asp:ListItem>
    </asp:DropDownList>

    二、后台动态绑定

    1.DataTable

    DataTable dt = new DataTable ();
    //中心思想就是将下拉列表的数据源绑定一个表(这里没有对表进行赋值)
    DropDownList1.DataSource = dt.DefaultView;
    //设置DropDownList空间显示项对应的字段名,假设表里面有两列,一列绑定下拉列表的Text,另一列绑定Value
    DropDownList1.DataValueField = dt.Columns[0].ColumnName;
    DropDownList1.DataTextField = dt.Columns[1].ColumnName;
    DropDownList1.DataBind();
     三、自定义添加

      /方法一:分步进行
    ListItem li = new ListItem();
    li.Text = "南京";
    li.Value = "1";
    DropDownList1.Items.Add(li);
    //方法二:ListItem()第一个参数是Text的值,第二个参数是Value的值
    ListItem li = new ListItem("扬州", "2");
    DropDownList1.Items.Add(li);
    //方法三:一步到位
    DropDownList1.Items.Add(new ListItem("徐州", "3"));
    //方法四:(循环添加)
    string[] city={"南京","扬州","徐州","苏州"};
    for(int i=0;i<city.Length;i++)
    {
        DropDownList1.Items.Insert(i,city[i]);
        DropDownList1.Items[i].Value = i.ToString();
    }


               
               

  • 相关阅读:
    在ASP.Net和IIS中删除不必要的HTTP响应头
    Json对象与Json字符串互转
    Jquery ajax传递复杂参数给WebService
    HTTP的KeepAlive是开启还是关闭?
    MQ产品比较-ActiveMQ-RocketMQ
    RocketMQ(7)——通信协议
    mq使用经验
    mq
    RocketMQ
    发送短信验证码实现方案
  • 原文地址:https://www.cnblogs.com/weimingxin/p/8541071.html
Copyright © 2011-2022 走看看