使用DataSource属性指定要绑定到数据列表控件的值的源。

数据源必须是实现System.Collections.IEnumerable接口(例如System.Data.DataView、system.Collections.ArrayList或System.Collections.Hashtable)

               或IListSource接口的对象。(如:dataset,datatable,dataview)

1、List<T>数据源

  

List
List<string> li=new List<string>();
For(int i=0;i<=10;i++)
{
      li.add(i.tostring());  
}
xxxx.DataSource=li;
xxxx.DataBind();

 

List<类>
自定义一个类
public class MyClass
{
    public MyClass()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private string _Name;
    private string _Home;
    public string Name
    {
        set { _Name = value; }
        get { return _Name; }
    }
    public string Home
    {
        set { _Home = value; }
        get { return _Home; }
    }
}


List<MyClass> li=new List<MyClass>();
For(int i=0;i<=10;i++)
{
      MyClass cl=new MyClass();
      cl.Name=i+"Name";
      cl.Home=i+"Home";
      li.Add(cl);
}
DropDownList1.DataSource=li;
DropDownList1.DataTextField="Name";
DropDownList1.DataBind();

 

 

相关文章:

  • 2021-11-02
  • 2022-12-23
  • 2021-09-27
  • 2021-11-01
  • 2022-01-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2021-08-23
  • 2021-11-01
相关资源
相似解决方案