Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use LINQ to find selected items from Listbox in ASP.NET

Author: Rudy Pinson
Download Source Code : 1305_LINQSelectedListboxItems.zip

LINQ has many operators for richer query capabilities. You will learn how to use LINQ query to find selected items from Listbox control in this article.

 

LINQ has so much to explore that I have written a series of articles on it and you can read these articles on DevASP.NET. Using LINQ in your application has many advantages which I have discussed in previous articles so keep learning LINQ on DevASP.NET.
 
You can use old style of foreach loop to get selected items from ListBox but LINQ query is a better way to accomplish this need. Below example will show how you can do it. Source code for the example is available to download.
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Now add code below in aspx page

    <asp:Label ID="Label1" runat="server" Text="Select Items:"></asp:Label>
       <br />
       <asp:ListBox ID="ListBox1" runat="server" Height="100px"
            SelectionMode="Multiple" Width="100px">
           <asp:ListItem>USA</asp:ListItem>
           <asp:ListItem>UK</asp:ListItem>
           <asp:ListItem>Canada</asp:ListItem>
           <asp:ListItem>Germany</asp:ListItem>
           <asp:ListItem>France</asp:ListItem>
       </asp:ListBox>
       <br />
       <asp:Button ID="Button1" runat="server" Text="Click to see your selected Items"
                onclick="Button1_Click" />
       <br />
       <asp:ListBox ID="ListBox2" runat="server" Height="100px" Width="100px">
    </asp:ListBox>
     
    I have added a Label and a Button in the page. I have also added two ListBox controls. One ListBox control is to select items and other is to display selected items on button click. I have added List Items using Items property but you can bind ListBox to database data.
     
  4.  Now open code behind file and write code below in button click event. Make sure that “System.Linq” namespace exists in your code behind file.

    C#
     
    protected void Button1_Click(object sender, EventArgs e) {
        var selectedCountries = from item in ListBox1.Items.Cast()
                                           where item.Selected == true
                                           select item;
     
        ListBox2.Visible = true;
        ListBox2.DataSource = selectedCountries;
        ListBox2.DataBind();
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim selectedCountries = From item In ListBox1.Items.Cast(Of ListItem)()
                                            Where item.Selected = True
                                            Select item
     
        ListBox2.Visible = True
        ListBox2.DataSource = selectedCountries
        ListBox2.DataBind()
    End Sub
     
    You can see in LINQ query, we have to explicitly cast items to ListItem to use in query.  

     
  5. Same LINQ query can be written in Lambda expression like below

    C#
     
    var selectedCountries = ListBox1.Items.Cast().Where(item => item.Selected == true);
     
    ListBox2.Visible = true;
    ListBox2.DataSource = selectedCountries;
    ListBox2.DataBind();
     
    VB.NET
     
    Dim selectedCountries = ListBox1.Items.Cast(Of ListItem)().Where(Function(item) item.Selected = True)
     
    ListBox2.Visible = True
    ListBox2.DataSource = selectedCountries
    ListBox2.DataBind()

     
  6. Add below code in Page Load method of code behind file. This is only for the purpose to not show the second ListBox when page is requested first time. You can ignore this code.

    C#
     
    protected void Page_Load(object sender, EventArgs e) {
        if(!IsPostBack) {
            ListBox2.Visible = false;
        }
    }
     VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ListBox2.Visible = False
        End If
    End Sub
     
  7. View the Web Site in browser and see the result. Select List items and click on button to see selected items.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use Accordion Control of AJAX Control Toolkit in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net