温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET中DropDownList下拉框列表控件绑定数据的方法有哪些

发布时间:2021-09-16 16:33:11 来源:亿速云 阅读:323 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“ASP.NET中DropDownList下拉框列表控件绑定数据的方法有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ASP.NET中DropDownList下拉框列表控件绑定数据的方法有哪些”这篇文章吧。

DropDownList Web 服务器控件使用户能够从预定义的列表中选择一项。它与 ListBox Web 服务器控件的不同之处在于,其项列表在用户单击下拉按钮之前一直处于隐藏状态。另外,DropDownList 控件与 ListBox 控件的不同之处还在于它不支持多重选择模式。

DropDownList在html中的呈现对应的是select,下面让我们来看一下DropDownList绑定数据的几种方法。

一、把Array数组绑到DropDownList

string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };
this.DropDownList1.DataSource = Month;
this.DropDownList1.DataBind();

这种方法只可以绑定一组数据到DropDownList,因为DropDownList可以绑定两种数据:1是DataTextField、2是DataValueField,所以第一种方法绑定后DataTextField的值==DataTextField值。

二、把动态Array数组绑定到DropDownList

ArrayList ar = new ArrayList();
for (int i = 1; i <=12; i++)
{
    ar.Add(i+"月");
}
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();

本质上就是讲1到12月加到数组中,如下:

ArrayList ar = new ArrayList();
ar.Add("1月");
ar.Add("2月");
ar.Add("3月");
ar.Add("4月");
...
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();

这种方法的好处是通过ArrayList.Add的方法,可以实现动态添加元素的功能,比方说,有一个DataTable,我们要把DataTable中一行的数据读出来添加到Arraylist当中。

看我以下的示的代码

ArrayList ar = new ArrayList();
DataTable dt=dataset.Tables[0]
foreach (DataRow dr in dt.Rows)
{
    ar.Add(dr[0].ToString());
}

以上代码从一个DataTable中通过foreach语句循环读取Table中一行数据中第一个格的值添加到ArrayList当中。

三、将Hashtable绑定到Dropdownlist当中Hashtable的方法的好处是,它也可以绑定两种数据一个是"key,一个是"value",这样的话,我们就可以为dropdonwlist绑定上两种不同的数据了。

Hashtable Ht = new Hashtable();
Ht.Add("January", "1月");
Ht.Add("February", "2月");
Ht.Add("March", "3月");
Ht.Add("April", "4月");
Ht.Add("May", "5月");
Ht.Add("June", "6月");
Ht.Add("July", "7月");
this.DropDownList3.DataSource = Ht;
this.DropDownList3.DataValueField = "key";
this.DropDownList3.DataTextField = "value";
this.DropDownList3.DataBind();

四、把Object对象绑定到dropdownlist

首先新增一个类,结构如下

public class ClassMonth
{
    private string _MonthEN = DateTime.Now.ToString("MMMM",System.Globalization.CultureInfo.CreateSpecificCulture("en"));
    private string _MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
    public ClassMonth()
    {
        MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
        MonthEN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("en"));
    }
    public ClassMonth(string cn,string en)
    {
        MonthCN = cn;//导入变量为属性赋值
        MonthEN = en;//导入变量为属性赋值
       
    }
    public string MonthEN //构造属性
    {
       get
        {
            return _MonthEN;
        }
        set
        {
            _MonthEN = value;
        }
    }
    public string MonthCN  //构造属性
    {
        get
        {
            return _MonthCN;
        }
        set
        {
            _MonthCN = value;
        }
    }
}

绑定方法

ArrayList arlist=new ArrayList();
arlist.Add(new ClassMonth("1月", "January"));
arlist.Add(new ClassMonth("2月", "February"));
arlist.Add(new ClassMonth("3月", "March"));
arlist.Add(new ClassMonth("4月", "April"));
arlist.Add(new ClassMonth("5月", "May"));
this.DropDownList4.DataSource = arlist;
this.DropDownList4.DataValueField = "MonthEN";
this.DropDownList4.DataTextField = "MonthCN";
this.DropDownList4.DataBind();

以上是“ASP.NET中DropDownList下拉框列表控件绑定数据的方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI