Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
I am developing a web application in C# .net4.0. In that i am using dropdown lists. Suppose,if i want to access 3rd item in the dropdown list and display it in a label,how can i do that(without using "selected index")?
To be more specific, i mean to ask is it possible to access a dropdown list in the form of array?? (i.e in an array, we can access a specific item like a[3]) Plz help me

Thanking you
Posted
Updated 29-May-12 1:07am
v2
Comments
bbirajdar 29-May-12 7:11am    
Any specific reason for not using SelectedIndex ?
jagadeeshmn 29-May-12 7:14am    
bcoz im dynamically adding items to that dropdown list.So to remove redundancy,i want to check whether the item im adding is already present in that dropdown list
bbirajdar 29-May-12 7:23am    
DropDownList1.Items.FindByValue(searchString); will work for you

You can access the elements of Dropdownlist as Dropdwnlist.items. this is the collection of all the items of the dropdownlist.
 
Share this answer
 
You can try this:

C#
string selectedText = _DropDownList.Items[_DropDownList.SelectedIndex].Text;
string selectedValue = _DropDownList.Items[_DropDownList.SelectedIndex].Value;


Or if you want to get the item with a particular index you can write as

C#
string selectedText = _DropDownList.Items[3].Text;
string selectedValue = _DropDownList.Items[3].Value;



Hope this helps.
 
Share this answer
 
You can get any list items value or text via the code below, just change the variable names

// sets the label to the list items Text value
nameOfLable.Text = ddlOne.Items[indexRequired].Text;

// sets the label to the list item Value 
nameOfLable.Text = ddlOne.Items[indexRequired].Value;
 
Share this answer
 
v2
You knows the answer.............

C#
string DrpDwnRequiredStr = a1[3];

also you can access it by your required index like....

C#
comboBox1.Items[2].ToString();
listBox1.Items[1].ToString(); 
 
Share this answer
 

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