2013-02-07 16 views
0

我正在尋找一種方式,它是如何綁定來自DataSource的數據可以通過ListControl Class上的Item屬性訪問。DataSource to ListItemCollection

任何人都可以給我一個示例如何從傳遞對象到DataSource的屬性綁定到泛型類ListItemCollection?如何通過代碼完成翻譯?

我想看到的翻譯是從DataSet到ListItemCollection。預先感謝您的幫助。

回答

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(); 

如果你的問題是關於如何在幕後完成綁定,那就有一個相當複雜的答案。

+0

是的,我的問題是如何在幕後做。我需要一個想法如何獲取DataTextField中提供的值,然後將其綁定到ListItem.Text字段..也許一些鏈接顯示任何示例 - 我正在搜索和選擇.. –

+1

您可能會看這篇文章:http:// msdn。 microsoft.com/en-us/magazine/cc163816.aspx。最後,它討論瞭如何在WebForms數據綁定中使用TypeDescriptors,並展示如何實現DataBinder.Eval函數。 –