Print Page | Close Window

Filtering Using Checkboxes.

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=16768
Printed Date: 28 March 2026 at 8:59pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Filtering Using Checkboxes.
Posted By: davidshq
Subject: Filtering Using Checkboxes.
Date Posted: 01 October 2005 at 4:32pm
I have ten different criteria by which I want individuals using my application to be able to filter. When they click off a filter I want it to automatically update the result set from SQL. I am using a GridView in ASP.NET 2.0. Here is an example of what I mean:
CheckBox1 - Apples
CheckBox2 - Oranges
CheckBox3 - Pears
Now, if they unselect CheckBox1, I want it to only give them results that match Oranges and Pears. If they unselect CheckBox 2,3 they should only get Apples results. Any ideas on how to do this?
I was initially going to do an If...Then statement for each of the checkboxes to create the SQL, but I realized I would have to create dozens of them to handle all the possibilities.
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Replies:
Posted By: michael
Date Posted: 03 October 2005 at 8:59am
Use a checkboxlist and then iterate through the items adding the checked ones to your query...
 

<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True">

<asp:ListItem>Pears</asp:ListItem>

<asp:ListItem>Apples</asp:ListItem>

<asp:ListItem>Oranges</asp:ListItem>

<asp:ListItem>Citrus</asp:ListItem>

</asp:CheckBoxList>

 

Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged

For Each item As ListItem In CheckBoxList1.Items

If item.Selected Then

'add it to your SQL Query

End If

Next

End Sub



-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: davidshq
Date Posted: 08 October 2005 at 12:26pm
Thanks Michael. That was a great help.
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: davidshq
Date Posted: 17 October 2005 at 11:02pm
One more question on filtering. Here is my new code:
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
        For Each item As ListItem In CheckBoxList1.Items
            gamesSqlDataSource.SelectParam eters("varSearch").DefaultValue = 1
            If item.Selected Then
                If item.Value = 1 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varAI").DefaultValue = 1
                End If
                If item.Value = 2 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varSI").DefaultValue = 1
                End If
                If item.Value = 3 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varII").DefaultValue = 1
                End If
                If item.Value = 4 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varCG").DefaultValue = 1
                End If
                If item.Value = 5 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varAO").DefaultValue = 1
                End If
                If item.Value = 6 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varSO").DefaultValue = 1
                End If
                If item.Value = 7 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varIO").DefaultValue = 1
                End If
                If item.Value = 8 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varWG").DefaultValue = 1
                End If
                If item.Value = 9 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varMG").DefaultValue = 1
                End If
                If item.Value = 10 Then
                &n bsp;   gamesSqlDataSource.SelectParameters("varTrick").DefaultValue = 1
                End If
            End If
        Next
    End Sub
My problem is that I also need it to check if an item is not checked. E.g., if checkbox for varTrick is unchecked then it needs to set gamesSqlDataSource.SelectParameters("varTrick").DefaultValue = 0. Otherwise, when I check the checkboxes everything works fine but when I uncheck them nothing changes. Any ideas?
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: michael
Date Posted: 18 October 2005 at 1:51pm
you can just put an else clause in there...

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: davidshq
Date Posted: 18 October 2005 at 11:27pm
Good idea. I didn't think that was going to work. But after just spending the last few minutes writing a post to tell you why it wasn't going to work, I saw your logic and will let you know if it doesn't work...But I think it will. Thanks.
David.


-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net