Bind dropdownlist with integers
I have an int variable, e.g.
int i = 100;
What I want to do is binding a ddl with 100 listitems, from 1 to 1开发者_如何学编程00. I could cycle the variable and for each number adding a ListItem to the ddl, but I'd like to know if there's an alternative, something like value the DataSource with the variable.
Thanks
int startingItem = 1;
int numberOfItems = 100;
IEnumerable<int> bindingSource = Enumerable.Range(startingItem, numberOfItems);
If the text and value of each ListItem should be the same just use:
myDropDownList.DataSource = myListOfInts;
myDropDownList.DataBind();
Alternatively, you can use Linq for a more complex setup
myDropDownList.DataSource =
from i in myListOfInts
select new ListItem("My Num: " + i, i.ToString());
myDropDownList.DataBind();
精彩评论