ASP.NET - how to get id from listbox - Asked By msakt on 07-Jun-12 02:34 AM

i am using listbox bind  id and i sekect multiple value now i want id from particular value
Chintan Vaghela replied to msakt on 07-Jun-12 02:38 AM

Hi Frndz,


Functionality ::Get selected item in LIstbox ( multiple)


Foreach loop through listbox. And get selected item as folloiwg  way





  foreach (ListItem item in lstFirst.Items)


          string selectedvalue = "";

          if (item.Selected)


            selectedvalue = item.Value;



Hope this helpful!



Jitendra Faye replied to msakt on 07-Jun-12 02:42 AM
Use this code-

int id= Convert.ToInt32( ListBox1.SelectedValue);
DL M replied to msakt on 07-Jun-12 05:02 AM

use below code
.aspx page code
<asp:Button ID="btn" Text="Get Value" OnClick="btn_Click" runat="server" />

<asp:ListBox ID="ls" runat="server" SelectionMode="Multiple">

.cs file code
protected void btn_Click(object sender, EventArgs e)
      string s = "";
      foreach (ListItem li in ls.Items)
        if (li.Selected)
          s += li.Text + ",";