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

    第二种:在后台动态绑定

    复制代码
    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();
    }
    复制代码
  • 相关阅读:
    java实现九九乘法表
    for循环的阶乘
    Struts2 表单提交与execute()方法的结合使用
    Struts2 第三个程序 namespacce的用法
    java 字符串的比较compareTo
    java中的位预算
    java调用C++ DLL库方法
    Socket编程模式理解与对比
    c 高级函数的简单用法
    TCP粘包分析与处理
  • 原文地址:https://www.cnblogs.com/asdyzh/p/9756974.html
Copyright © 2011-2022 走看看