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 value.....here 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

 

 

CODE ::

 

  foreach (ListItem item in lstFirst.Items)

        {

          string selectedvalue = "";

          if (item.Selected)

          {

            selectedvalue = item.Value;

          }

        }

Hope this helpful!

Thanks

 

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
Hi..

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">
        <asp:ListItem>Item1</asp:ListItem>
        <asp:ListItem>Item2</asp:ListItem>
        <asp:ListItem>Item3</asp:ListItem>
        <asp:ListItem>Item4</asp:ListItem>
        <asp:ListItem>Item5</asp:ListItem>
      </asp:ListBox>

.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 + ",";
        }
      }
      Response.Write(s);
    }