[Asp.Net]4种方法把数据绑定到Dropdownlist

第一种,把Array数组绑到dropdownlist

程序代码

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

第一种方法只可以绑定一组数据到dropdownlist,因为drawdonwlist可以绑定两种数据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();

直观一点的写法。

程序代码

ArrayList ar = new ArrayList();
                    ar.Add("1月");
                    ar.Add("2月");
                    ar.Add("3月");
                    ar.Add("4月");
..................................................

this.DropDownList2.DataSource = ar;
        this.DropDownList2.DataBind();

第2种方法的好处是通过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();

第4种,把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();

时间: 2024-12-30 18:58:26

[Asp.Net]4种方法把数据绑定到Dropdownlist的相关文章

【转】4种方法把数据绑定到Dropdownlist

第一种,把Array数组绑到dropdownlist 程序代码 string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };        this.DropDownList1.DataSource = Month;        thi

ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)

转自:http://www.cnblogs.com/zyqgold/archive/2010/11/22/1884779.html 在ASP.NET MVC框架中,将视图中的数据传递到控制器中,主要通过发送表单实现的.具体使用中,主要使用以下三种方法. 1.通过Request.Form读取表单数据        2.通过FormCollection读取表单数据        3.直接读取表单数据对象 下边是我学习这些东西时的一点总结 1.通过Request.Form读取表单数据      首先定

asp.net 几种传值方法的分析

本文转自:http://www.cnblogs.com/shengtianlong/archive/2010/08/11/1797608.html ASP.NET页面传值方法的优缺点及适用范围 1. Get(即使用QueryString显式传递)     方式:在url后面跟参数.     特点:简单.方便.     缺点:字符串长度最长为255个字符:数据泄漏在url中.     适用数据:简单.少量.关键的数据.     适用范围:传递给自己.传递给另一个目标页面:常用于2个页面间传递数据.

asp.net mvc3利用Areas分离前后台的4种方法

一.引言 : 为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际的开发中,随着项目规模的不断扩大,Controller控制器也随之不断增多.如果在Controllers文件夹下面有超过两位数controller,即便采用良好的命名规范,或者用子文件夹的形式区分不同功能的控制器,还是会影响项目的可阅读性和可维护性.因此,在一些场景下,如果能把与某功能

【转】Asp.net中时间格式化的6种方法详细总结

1. 数据控件绑定时格式化日期方法: 代码如下: <asp:BoundColumn DataField="AddTime" HeaderText="添加时间" DataFormatString="{0:yyyy-MM-dd HH:mm}></asp:BoundColumn> <asp:BoundField DataField="AddTime" HeaderText="添加时间" Dat

WPF 数据绑定,界面刷新的两种方法-----INotifyPropertyChanged

.Netformwork4.0及以下版本 -------INotifyPropertyChanged 命名空间: System.ComponentModel 后台代码 public partial class DvrWnd : UserControl { public DvrWnd() { InitializeComponent(); } private void InitInfo() { for (int i = 0; i < 10; i++) { DvrInfo dvrInfo = new

ASP.NET MVC 实现AJAX跨域请求的两种方法

通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框架里实现跨域的AJAX请求有几种方式可以实现,以下就介绍常用的两种方法. 1.     发送JSONP请求 客户端: JQuery对发送JSONP请求有很好的支持,客户端通过. ajax() 函数发送请求,其中需要制定 dataType 为“jsonp”  jsonpCallback 为指定的回调函

ASP.NET输出EXCEL表格的几种方法(总结修改)

这几天要从数据库导出EXCEL表格,就找了N钟方法,经测试,下面的方法比较的好用一点.都是经过输入DataTable而导出的.不过导出的EXCEL都不是正规的EXCEL格式,只能说是HTML格式的,导出的再导入进数据库就会出现问题了.想导入的话用EXCEL打开另存为EXCEL格式就好了 1.利用DataRow直接输出,经测试,没有乱码.        public bool LendOutExcel(string strFileName, DataTable DT)        {      

asp.net(c#)网页跳转七种方法小结

1.response.redirect  这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次 postback),但他可以跳 转到任何页面,没 有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护.但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个 http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端.需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session. 实例 : using System; using Syst