• DropDownList添加总结


    在.net开发中常使用的DropDownList个人总结如下:

    1.最常见的是手动添加控件Item,这里大家都会就不多说了。

    2.还有一种是绑定DATETABLE的 个人感觉不错绑定分两种方式

    e.g.: Private void FillCurrencyDDL()                                                                              

    {

          DataTable dt = GetCurrencyDT();

          if(dt!=null)

               {

                   foreach(DateRow row in dt.rows)

                   {

                     this.ddl.Items.Add(new ListItem(row["Name"].ToString(),new Guid(row["ID"].ToString()).ToString())) //这里要求GetCurrencyDT()方法返回的值有Name和ID两项

                   }

              }

    }

    另外一种绑定方法

    DateSet ds = new DataSet();

    ds = GetddlDateSet();

    this.ddl.DataSource = ds.Table[0].DefaultView;

    this.ddl.DataTextField ="name";

    this.ddl.DataValueFiled="ID";

    this.ddl.DataBind();

    3.绑定已知的List

    e.g. : //首先写一个LIST的方法

    public static List GetList()

    {

        List sCurrency = new List();

       sCurrency.Add("RMB");

       sCurrency.Add("USD");

       sCurrency.Add("JPY");

       Retrun sCurrency;

    }

    private void FillDDL()

    {

        ValueList currencyList = this.ddl.ValueList;

        List sCurrencyList=GetDDL();

        foreach(string currency in sCurrencyList)

       {

             ValueListItem item =new ValueListItem(Currency);

             currencyList.ValueListItems.Add(item);

       }//此方法将DDL的Value和Text都会赋入一个值“Curreny”

       //这种方法一定要写在if(!IsPostBack){}中 否则选择后页面会始终重新加载,显示始终是第一项

    }

    4.另外附送一个年份的DDL控件写法 个人觉得还不错

    public void FillDDL()

    {

           List yearlist = new List();

           base.Items.Clare();

           if(IsNullable)

              base.Items.Add(new ListItem("AllYeas","0"));

           int todayYear = DateTime.Today.Year;

           for(int i=todayYear; i>=todayYear-9;i--)

           yearList.Add(i);//将从今年的前十年放入列表中

           base.DataSource = yearList;

          base.DataBind();

    }

  • 相关阅读:
    [day002]剑指 Offer 09. 用两个栈实现队列
    [day003]718. 最长重复子数组
    [linux]关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
    96. 不同的二叉搜索树
    91. 解码方法
    [动态规划]64. 最小路径和
    62.不同路径
    【Java】list根据某一条件进行分组
    【Java】批量生成小程序参数码并打包下载
    【Docker】使用docker制作libreoffice镜像并解决中文乱码问题
  • 原文地址:https://www.cnblogs.com/zxjyuan/p/1546716.html
Copyright © 2020-2023  润新知