.net資料來源(DataSource)

來源:互聯網
上載者:User
.net中:
DropDownList,ListBox,CheckBoxList,RodioButtonList等控制項的.DataSource屬性可使用
ListItemCollection集合,注意:綁定時須指定DataValueField,DataTextField屬性的值!
不指定欄位後value和Text的值都為Text.

ListItemCollection listItems = new ListItemCollection();
listItems.Add(new ListItem("測試資料一", "1"));
listItems.Add(new ListItem("測試資料二", "2"));
RadioButtonList1.DataSource = listItems;
/*指定欄位值,不指定預設為:
RadioButtonList1.DataValueField = "Text";
RadioButtonList1.DataTextField = "Text";
*/
RadioButtonList1.DataValueField = "Value";      //指定控制項Value欄位值
RadioButtonList1.DataTextField = "Text";        //指定控制項Text欄位值
RadioButtonList1.DataBind();

資料控制項:Repeater,DataList,DetailView,GridView等都可以直接綁定資料來源為泛型資料,如:
List<>,實現IList<>介面資料。public class person
{
    private string name;
    private int age;

    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
    public person(string name,int age)
    {
        this.name = name;
        this.age = age;
    }
}
//使用資料控制項:
IList<person> testList = new List<person>();
testList.Add(new person("張三",31));
testList.Add(new person("李四",17));

Repeater1.DataSource = testList;
Repeater1.DataBind();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.