Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Filtering Using Checkboxes.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Filtering Using Checkboxes.

 Post Reply Post Reply
Author
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post Topic: Filtering Using Checkboxes.
    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.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post Posted: 08 October 2005 at 12:26pm
Thanks Michael. That was a great help.
David.
Back to Top
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2005 at 1:51pm
you can just put an else clause in there...
Back to Top
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.