ASP.NET - how to get id from listbox - Asked By msakt on 04-Jun-12 06:15 AM

in listbox have multiple i want get id from vaue in listbox
S K replied to msakt on 04-Jun-12 06:19 AM
Simply get the id of selected item from the listbox control  by this line
int id  = listboxcontrolid.Selected.Value;

Only make sure at the time of binding listbox you should bind Value field with id column than you will get id by above pice of code.

hope this help you
dotnet freak replied to msakt on 04-Jun-12 06:20 AM
Use JQuery

 <select name="List_0" size="4" id="List_0" multiple="multiple">
        <option value="Komal ">Komal </option>
        <option value="Ranjeet">Ranjeet</option>
        <option value="Vishal ">Vishal </option>
        <option value="Gaurav">Gaurav</option>
        <option value="Dhanpat">Dhanpat</option>
    <input type="button" id="btnSelectedItems" value="Selected Items" />
Here is the jquery script:

$('#btnSelectedItems').click(function() {
smr replied to msakt on 04-Jun-12 06:22 AM

Try this

The code below is from the code behind and it allows you to get multiple selected values from the listbox and pass it to the string variable called DriveWheels. 

string DriveWheels = string.Empty;
 for (int i = 0; i <= LstDriveWheels.Items.Count – 1; i++)
    if (LstDriveWheels.Items[i].Selected && LstDriveWheels.Items[i].Value != “-1?)
      DriveWheels += LstDriveWheels.Items[i].Value + “,”;


Jitendra Faye replied to msakt on 04-Jun-12 06:22 AM
Use this -

int intID  = listboxcontrolid.Selected.Value;

Lalit M replied to msakt on 04-Jun-12 07:02 AM

You can also get id from listbox using JQuery ..for code sample show this URL
kalpana aparnathi replied to msakt on 04-Jun-12 03:15 PM

Try below code:

private void getId()
    age.categories.ToList().ForEach((c) => listcategories.Items.Add(c));
    listcategories.SelectedIndexChanged += (sender, e) => {
      Category category = (Category)listcategories.SelectedItem;
      // Do something with category.Id