• 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>
    复制代码

    第二种:在后台动态绑定

    复制代码
    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();
    }
    复制代码
  • 相关阅读:
    Zookeeper入门
    Redis五种数据类型
    shardingJDBC分库分表
    RabbitMQ入门
    Spring-Boot
    spring-mvc
    oracle一些对表的操作命令
    对IFeatureClass的选择结果进行再次选择及排序
    关于基础 DBMS 错误 ORA-01654的总结
    Linq 如何实现 in 与 not in
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9756974.html
Copyright © 2020-2023  润新知