In this article I’m going to explain how to get multiple selected value in ListBox control using ASP.NET with C#.

What is ListBox ?

ListBox control to create a list control that allows single or multiple item selection. To enable multiple item selection, set the SelectionMode property to Multiple. ListItem element is used to specify the items between opening and closing tags of the ListBox control. ListBox control also support data binding.

Here I’ll show how to get multiple selected value in ListBox control.

I have used Northwind database to bind Product details to ListBox. Here you can download Northwind database

http://northwinddatabase.codeplex.com/releases/view/71634

Column Name

Data Type

ProductName

varchar(100)

ProductID

int

Get selected values:

Using foreach loop we can get multiple selected values in ListBox

        foreach (ListItem li in ListBox1.Items)

        {

            if (li.Selected)

            {

                lblValues.Text += li.Text+"<br/>";

            }

        }


Designer source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ListBox ID="ListBox1" runat="server" Height="200px" SelectionMode="Multiple"></asp:ListBox>

         <asp:Button ID="btnSubmit" runat="server" Text="Get Values" OnClick="btnSubmit_Click" /> 

       <asp:Label ID="lblValues" runat="server" ></asp:Label> 

   </div>

    </form>

</body>

</html>

 C#-code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

    SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Northwind;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindList();

        }

    }

     private void BindList()

    {

        DataSet ds = new DataSet();

        conn.open();

        string cmdstr = "select ProductName,ProductID from Products";

        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);

        adp.Fill(ds);

        ListBox1.DataSource = ds;

        ListBox1.DataTextField = "ProductName";

        ListBox1.DataValueField = "ProductID";

        ListBox1.DataBind();

        conn.close();

    } 

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        foreach (ListItem li in ListBox1.Items)

        {

            if (li.Selected)

            {

                lblValues.Text += li.Text+"<br/>";

            }

        }

    }

}

cytotec abortion

an abortion pill buy abortion pills online
medical abortion pill online cytotec abortion abortion pill


View demo Download
http://www.dotnetfox.com/Document/1047/0Demo1.gif

If you enjoyed this article, get email updates (it's free).



Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox