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

in listbox have multiple value....here 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>
    </select>
    <input type="button" id="btnSelectedItems" value="Selected Items" />
Here is the jquery script:


$('#btnSelectedItems').click(function() {
                alert($('#List_0').val());
            });
smr replied to msakt on 04-Jun-12 06:22 AM
Hi

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 + “,”;
     }
   }


refer
http://www.digitalwatermelon.com/1/setting-listbox-selectedvalues-in-asp-net/ 

http://forums.asp.net/t/1528995.aspx 

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

int intID  = listboxcontrolid.Selected.Value;

DL M replied to msakt on 04-Jun-12 07:02 AM
Hi..

You can also get id from listbox using JQuery ..for code sample show this URL

http://forums.asp.net/t/1706720.aspx/1?HOW+TO+GET+LISTBOX+ID+SELECTED+vALUE+using+jquery+
kalpana aparnathi replied to msakt on 04-Jun-12 03:15 PM
hi,

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
    }
}
Regards,