Click here to Skip to main content
12,634,712 members (23,521 online)
Rate this:
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 13:50pm
ThePhantomUpvoter 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
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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

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!.
Rate this: bad
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();
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Asp.Net listbox operations ....... Asp.Net ListBox


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 20 Feb 2014
Copyright © CodeProject, 1999-2016
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