DataSource到ListItemCollection

时间:2013-02-07 17:19:16

标签: c# asp.net web-controls

我正在寻找一种方法,如何通过ListControl类上的Item属性访问DataSource中的数据。

有人能举例说明从传递对象到DataSource的属性是如何绑定到泛型类ListItemCollection的?如何通过代码完成翻译?

我想看到的翻译是从DataSet到ListItemCollection。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

// Setting up a dataset.  This dataset has no data; in real life you'd get the
// data from somewhere else, such as a database, and wouldn't need to build it.

DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
dt.Columns.Add(new DataColumn("Description"));
ds.Tables.Add(dt);

// Creating a list box. You'd probably have this declared in your HTML and wouldn't need to
// create it.  

ListBox listBox1 = new ListBox();

listBox1.DataSource = ds.Tables[0];
listBox1.DataValueField = "ID";
listBox1.DataTextField = "Description";
listBox1.DataBind();

如果您的问题是关于如何在幕后进行绑定,那么答案相当复杂。

相关问题