Im iterating a list and getting hold of 2 fields "Name" and "Url"
i want to extract these fields and use them to create a datasource
I want to databind these to a dropdownList
DropDownList.DataTextField = "Name";
DropDownList.DataValueField = "Url";
how can I create a datasource based on this list? then do the above, then databind.
Assuming that you have an list item named MyListItem and MyListItem has two properties Name and Url, you can bind a list of MyListItem like that :
List<MyListItem> dataSource = new List<MyListItem>();
MyListItem item1 = new MyListItem();
item1.Name = "Name 1";
item1.Url = "Url 1";
dataSource.Add(item1);
MyListItem item2 = new MyListItem();
item2.Name = "Name 2";
item2.Url = "Url 2";
dataSource.Add(item2);
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = "Name";
dropDownList.DataValueField = "Url";
dropDownList.DataBind();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With