Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI......
How to read selected listbox value in asp.net
Posted
Comments
Ankur\m/ 11-Nov-10 2:26am    
I have left a reply. Please check.

ListBox1.SelectedValue

See this: ASP.NET ListControl Control[^]
 
Share this answer
 
Comments
raheleh_es 11-Nov-10 1:29am    
when I type this code return null

can you help me plz
my code:
for (int i = 0; i < ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected)
TextBox1.Text = ListBox1.Items[i].Text;
}


help me plz
Ankur\m/ 11-Nov-10 2:26am    
Sorry for a late reply. I was onto some work.

ListBox1.Items[i].Text would give you the text of the selected Item.
You actually don't need to navigate to each item if you need the selected text or value.
Just use ListBox1.SelectedValue for selected value or ListBox1.Text for selected text.

Put a debugger to see where the code is breaking and whose value is null.
look at this:

ListBox lst = new ListBox();
            lst.Items.Add("item1");
            lst.Items.Add("item2");
            lst.SelectedIndex = 0;
            string item = lst.SelectedValue;
 
Share this answer
 
Comments
Henry Minute 18-Nov-10 11:09am    
This does not answer the OPs question. Please stop trawling old questions and giving fatuous, incorrect answers in order to gain points.

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