Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys, im trying to get the ID of an item from the listbox and add it on to the database. does anyone know how can i go about it, i dont want the text. Ive tried selectedItem, and selectedValue but they not working. I get my listbox items from the database
Posted
Comments
[no name] 15-Sep-13 19:22pm    
In all of this confusion, are you , perhaps, looking for the SelectedIndex? We would have no idea what you think the "ID" is, what you are putting into your listbox or what you are getting out of your database.
syed shanu 15-Sep-13 21:49pm    
Do you store both Listbox text and Value .Is so then you can get the selected value of list box.Chek this link http://mrbool.com/working-with-listbox-using-asp-net-and-csharp/25298

Hi,

First make sure your are binding list box in this way.
C#
DataTable dtName = new DataTable();

    dtName.Columns.Add(new DataColumn("DisplayMember"));
    dtName.Columns.Add(new DataColumn("ValueMember"));

    dtName.Rows.Add("Suits", "0");
    dtName.Rows.Add("Harvey Spector", "1");
    dtName.Rows.Add("Jessica Pearson", "2");
    dtName.Rows.Add("Mike Ross", "3");
    dtName.Rows.Add("Donna Paulson", "4");
    dtName.Rows.Add("Rachel", "5");

    Listbox1.DataSource = dtName;
    Listbox1.DataTextField = "DisplayMember";//Name of Column
    Listbox1.DataValueField = "ValueMember";//Name of Column
    Listbox1.DataBind();


and on page load you are not rebinding the listbox control i.e place databinding code in if(!ispostback) block.
and simply call
C#
ListItem item = Listbox1.SelectedItem;
string selectedText = item.Text;
string selectedValue = item.Value;


Hope this helps!.
 
Share this answer
 
Simply You can do to get selected value

C#
foreach (ListItem item in ListBox1.Items)
      {
          if (item.Selected)
          {
              string value = item.Value.ToString();
          }
      }
 
Share this answer
 
Asp.Net listbox operations ....... Asp.Net ListBox

Patter
 
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