Click here to Skip to main content
15,884,928 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
If I store the following items in a DropDownList:
123
124
125
126
how can I store these dropdown list items into variables like below at Form load without selecting any of the items:
For example:
int a=dropdownlist1[0],
int b=dropdownlist1[1],
int c=dropdownlist1[2],
int d=dropdownlist1[3],
How can this happen dynamically at page load time using C# ? please help me..
Posted
Updated 13-Apr-14 9:10am
v3
Comments
ZurdoDev 11-Apr-14 12:40pm    
You can loop through them but why are you doing this?
Member 10624163 11-Apr-14 23:48pm    
its for different way of verify the online test result.
Member 10624163 11-Apr-14 23:50pm    
loop means which kind of loop statement is flexible for this and how to use that?
ZurdoDev 12-Apr-14 8:50am    
I still have no real idea of what your question is.
Herman<T>.Instance 11-Apr-14 13:20pm    
Use SelectedIndex prop and SelectedIndexChanged Event

I suggest to use array, as it will consume less memory space than the individual variables. You can store all the values in the array, As i have shown below.

Dropdown that you are going to use, should be bind (Should be filled) before using below code

C#
string[] arrItems = new string[dropdown.Items.Count];
    if (dropdown.Items.Count > 0)
    {
        for (int i = 0; i < dropdown.Items.Count; i++)
        {
            arrItems[i] = dropdown.Items[i].ToString();
        }
    }


Hope this will help you.
 
Share this answer
 
v2
Comments
Rahul VB 12-Apr-14 5:20am    
Good one
Member 10624163 13-Apr-14 10:55am    
Thank u Sir, Its very useful for me.....
Rahul VB 13-Apr-14 15:01pm    
Firstly dont call me Sir, we are friends here. The code which i wrote can be improved further, you must find out how to improve it and let me know. this is just for your homework so that you learn.

- Rahul
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.

C#
protected void Page_Load(object sender, EventArgs e)
  {

      if (!IsPostBack)      ////this is not required in this scenario......
      {
      drop.DataSource = new List<int> { 0, 1, 2, 3, 4 }; ///i have assigned an array of ints to the datasource.
          drop.DataBind();       /// bind it to the control
          int[] array = new int[drop.Items.Count]; ///i have used an array to store the items of the collection.

          foreach(ListItem item in drop.Items) // iterate through all items
          {
              array[n] = int.Parse(item.Value); /// start storing
              Response.Write("Item:" + n + "  Value :" + array[n].ToString() + ",");///print the data in the format Item:0 Value:0
              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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900