Bind a DropDownList to IEnumerable
I have a method:
public static IEnumerable<Datarow> getInfo (string test)
{
some functionality and adds two columns in datatable dt.
now how can i return dt f开发者_开发技巧rom this method (question 1)
}
I have to bind the method returned value to a dropdown list named ddlist
.
How can i make it possible
(question no 2.) .. when i tried i get the message that can not bind ienumerable . . . .
please help me out.
You could change the method to return a datatable or loop through the IEnumerable and add the list items manually.
Check this blog post for another approach to what you're trying to do. http://geekswithblogs.net/mikethomas/archive/2007/01/15/103686.aspx
Code sample from blog (not mine):
public IEnumerable GetDataSource() {
string key = "CodeDrowDownListTest_" + CodeName;
object item = Page.Cache.Get(key);
if (item == null)
{
item = GetDataFromDB();
Page.Cache.Insert(key, item, null, System.DateTime.UtcNow.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
}
return (IEnumerable)item; }
public override void DataBind() {
this.DataSource = GetDataSource();
this.DataTextField = "Text";
this.DataValueField = "Value";
base.DataBind();
}
精彩评论