Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
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 15-Sep-13 12:50pm
Comments
ThePhantomUpvoter at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
First make sure your are binding list box in this way.
 	        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
            ListItem item = Listbox1.SelectedItem;
            string selectedText = item.Text;
            string selectedValue = item.Value;
 
Hope this helps!.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Simply You can do to get selected value
 
foreach (ListItem item in ListBox1.Items)
      {
          if (item.Selected)
          {
              string value = item.Value.ToString();
          }
      }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Asp.Net listbox operations ....... Asp.Net ListBox
 
Patter
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 756
1 OriginalGriff 345
2 Magic Wonder 131
3 Kishore Pogaru 130
4 Fredrik Bornander 130
0 OriginalGriff 5,705
1 Sergey Alexandrovich Kryukov 4,763
2 CPallini 4,570
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100