Click here to Skip to main content
11,584,040 members (42,974 online)
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 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 334
2 Richard Deeming 220
3 Dave Kreskowiak 200
4 virusstorm 194
0 OriginalGriff 1,623
1 Sergey Alexandrovich Kryukov 1,135
2 Abhinav S 701
3 Dave Kreskowiak 627
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 20 Feb 2014
Copyright © CodeProject, 1999-2015
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