locked
Filtering Gridview with CheckboxList RRS feed

  • Question

  • Filtering asp.net gridview using checkbox list.

    I am having trouble putting all of the peices together.  I have an asp.net dropdownlist, checkbox list, and a gridview on my page.  It was previously 2 dropdown lists and the gridview.  What I want do to is let the users select via checkboxes the items they want to show in the gridview.

    CheckBoxList1 is the organization type
    CheckBoxList2 is the contact type
    Gridview1 shows the details about the person

    I am having trouble passing the values of the checkboxes to the sql stored procedure, and/or getting the stored procedure to accept the parameters.  Below is my code.  Any help would be apprecitated.

    <%@ Page Language="vb" Inherits="CimroIndyApps.Peoples" CodeFile="NewMyPeople.aspx.vb" EnableEventValidation="false" %>
    <%@ Register Src="CimroHeader.ascx" originalAttribute="Src" originalPath="CimroHeader.ascx" TagName="CimroHeader" TagPrefix="uc1" %>
    <%@ Register Src="LeftSideMenu.ascx" originalAttribute="Src" originalPath="LeftSideMenu.ascx" TagName="LeftSideMenu" TagPrefix="uc2" %>
    <%@ Register Src="Footer.ascx" originalAttribute="Src" originalPath="Footer.ascx" TagName="Footer" TagPrefix="uc3" %>
    <!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 id="Head1" runat="server">
    <title>MyPeople.aspx - My People</title>

    </head>

    <body>
    <form id="form1" runat="server">
    <div>
    <table style="width: 700px">
    <tr>
    <td>
    <uc1:CimroHeader ID="Header1" runat="server" />
    </td>
    </tr>
    </table>
    </div>
    <div>
    </div>
    <TABLE id="Table2" cellSpacing="1" cellPadding="1" border="0" style="width: 704px">
    <TR vAlign="top" >
    <TD vAlign="top" width="41" style="height: 112px"><uc2:LeftSideMenu id="LeftSideMenu1" runat="server"></uc2:LeftSideMenu></TD>
    <TD style="height: 112px; width: 684px;">
    <table style="width: 560px">
    <tr>
    <td align="left" valign="top" style="font-size: 9pt">
    <asp:Label ID="Label2" runat="server" CssClass="pagetitle" Text="My People Page"
    Width="325px"></asp:Label>&nbsp;
    <br />
    <br />
    Create a list of Contact Types for export.&nbsp; Choose the Setting and the Contact
    Type.<br />
    Then press the "Show Me the People" button.&nbsp; To export the data, choose "Export
    to Excel".<br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    &nbsp;&nbsp;&nbsp;&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
    <asp:PostBackTrigger ControlID="btnExportGrid" />
    </Triggers>
    </asp:UpdatePanel>
    &nbsp;<br />
    Choose Setting<br />
    &nbsp;<asp:CheckBoxList ID="CheckBoxList2" runat="server" DataSourceID="SqlDataSource2">
    </asp:CheckBoxList><br />
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
    <br />
    Choose Contact Type<br />
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource4"
    DataTextField="ContactType" DataValueField="ContactType" RepeatColumns="4" RepeatDirection="Horizontal"
    Width="800px">
    </asp:CheckBoxList><br />
    <asp:Button ID="Button1" runat="server" Text="Show Me the People!" />&nbsp;
    <asp:Button ID="btnExportGrid" runat="server" OnClick="BtnExportGrid_Click" Text="Export to Excel" /><br />
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
    DisplayAfter="0">
    <ProgressTemplate>
    <span style="background-color: #ffff99">SearchingPlease Wait..............</span>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/images/UpdateAnimation.gif" />
    </ProgressTemplate>
    </asp:UpdateProgress>
    &nbsp;<br />
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
    CellPadding="4" DataSourceID="SqlDataSource1" Font-Names="Verdana" Font-Size="X-Small"
    ForeColor="Black" GridLines="Vertical" Style="font-size: x-small; font-family: Verdana, Arial, Tahoma">
    <FooterStyle BackColor="#CCCC99" />
    <Columns>
    <asp:BoundField DataField="OrgID" HeaderText="OrdID" SortExpression="OrgID" />
    <asp:BoundField DataField="FullName" HeaderText="FullName" SortExpression="FullName" />
    <asp:BoundField DataField="ContactType" HeaderText="ContactType" SortExpression="ContactType" />
    <asp:BoundField DataField="OrgName" HeaderText="OrgName" SortExpression="OrgName" />
    <asp:BoundField DataField="OrgType" HeaderText="OrgType" SortExpression="OrgType" />
    <asp:BoundField DataField="OrgType2" HeaderText="OrgType2" SortExpression="OrgType2" />
    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
    <asp:TemplateField HeaderText="EMail" SortExpression="EMail">
    <EditItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("EMail") %>'></asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("email","MAILTO:{0}") %>'
    Style="font-size: x-small; font-family: Verdana, Arial, Tahoma" Text='<%# Eval("email") %>'></asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
    <asp:BoundField DataField="Fax" HeaderText="Fax" ReadOnly="True" SortExpression="Fax" />
    <asp:BoundField DataField="AddrLine1" HeaderText="AddrLine1" SortExpression="AddrLine1" />
    <asp:BoundField DataField="AddrLine2" HeaderText="AddrLine2" SortExpression="AddrLine2" />
    <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
    </Columns>
    <RowStyle BackColor="#F7F7DE" />
    <EmptyDataTemplate>
    &nbsp;No results found. Try another search. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    </EmptyDataTemplate>
    <EditRowStyle Font-Names="Verdana" Font-Size="XX-Small" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    &nbsp; &nbsp;&nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="CimroIndy_GetMailingList3" SelectCommandType="StoredProcedure">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="OrgType" PropertyName="SelectedValue"
    Type="String" />
    <asp:ControlParameter ControlID="CheckBoxList1" Name="ContactType" PropertyName="SelectedValue"
    Type="String" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [OrgType] FROM [OrgType]"></asp:SqlDataSource>
    &nbsp;
    <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [ContactType] FROM [ContactType]"></asp:SqlDataSource>

    <br />
    &nbsp;</td>
    </tr>
    </table>
    &nbsp;

    </TABLE>
    </form>
    </body>
    </html> 



    Imports System
    Imports
    System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    Namespace CimroIndyApps
    Partial Class Peoples
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeComponent()

    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    End Sub

    Protected Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    GridView1.Visible = True

    Dim msg As String

    Dim li As ListItem

    msg = ""

    For Each li In CheckBoxList1.Items

    If li.Selected = True Then

    msg = msg & "<br>" & li.Text & ", "

    End If

    Next

    Label3.Text = msg

    End Sub

    Protected Sub btnExportGrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportGrid.Click

    ' pass the grid that for exporting ...

    GridViewExportUtil.Export("Datagrid.xls", Me.GridView1)

    End Sub

    End Class

    End Namespace

    SELECT     p.FullName, c.ContactType, o.Name AS 'OrgName', o.City, o.State, ISNULL(p.EMail, 'email missing') AS 'EMail', ISNULL(p.Phone, 'phone missing')
                          AS 'Phone', ISNULL(p.Fax, 'fax missing') AS 'Fax', o.AddrLine1, o.AddrLine2, o.Zip, o.OrgType, p.OrgID, o.OrgType2
    FROM         People AS p INNER JOIN
                          Organization AS o ON p.OrgID = o.OrgID INNER JOIN
                          ContactTypeID AS c ON p.ID = c.PeopleID
    WHERE c.ContactType IN (SELECT c.ContactType FROM ContactType WHERE c.ContactType = @contacttype)
      
    ORDER BY o.OrgType, c.ContactType, p.FullName
    • Moved by Mike Walsh FIN Wednesday, September 24, 2008 5:38 AM Off-Topic (Moved from MSDN and TechNet Search Feedback to Off-Topic Posts (Do Not Post Here))
    Tuesday, September 23, 2008 4:32 PM

Answers

All replies

  • As this isn't a post about Search in MSDN or TechNet, it's Off-Topic.

    Moving to the Off-Topic forum. Please look for a more appropriate forum to post it to.
    Wednesday, September 24, 2008 5:37 AM
  •  

    Hello Jeremy, 

    Thank you for your post!  I would suggest posting your question in one of the 

     located here:  http://forums.asp.net/18.aspx.

    Have a great day!


    Shrikant Maske Tier 2 Application Support Server and Tools Online Engineering Live Services Team
    • Proposed as answer by Shrikant Maske Wednesday, October 22, 2008 3:15 PM
    • Marked as answer by Shrikant Maske Monday, March 23, 2009 12:30 PM
    Wednesday, October 22, 2008 3:15 PM