Hello Friend,
Ok so by what i understand you need to iterate through a collection of datasource items and store them into a separate variable for further analysis:
I agree with RyanDev however i will show you the code:
I will start with the code behind directly, because you can declare a drop down list in the aspx.
Please note i am using programatic databinding and not declarative:
Id used by me for the dropdownlist is
Quote:
drop
hence i will use that name to refer the drop down list control.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
drop.DataSource = new List<int> { 0, 1, 2, 3, 4 }; drop.DataBind(); int[] array = new int[drop.Items.Count];
foreach(ListItem item in drop.Items)
{
array[n] = int.Parse(item.Value); Response.Write("Item:" + n + " Value :" + array[n].ToString() + ","); n++;
}
}
}
This will work.
I have some of my doubts:
why do you need a seperate variable to store every item in the collection, because then you will be needing those many variables at runtime....
Indirectly, i want to say use an array.
However my code is very badly written, its very redundant. There was no need to take an array to store items.
I can use the Items property of the DropDownList collection to access the data, no need to use separate variables, no use of an array. I can directly traverse through the collection of Items.
Every thing above is dynamic as you said,
This code however should serve your purpose. Please let me know if it doesn't run.
Thanks a ton,
Rahul