Click here to Skip to main content
15,843,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to filter a gridview in asp with sqldatasource filterexpression and parameters. I can't find what I'm missing. When I change the drop downs nothing happens. Any help would be greatly appreciated as I'm new to ASP.

Here is the dropdownlist:
XML
<asp:DropDownList ID="dl_filter_Inst" runat="server" DataSourceID="ds_Inst"
                DataTextField="Num" DataValueField="Num" width="98%"
                AppendDataBoundItems="true">
                <asp:ListItem Selected="True" Value="">Show All</asp:ListItem>
                </asp:DropDownList>


Here is the datasource and filterparameters:
XML
<asp:SqlDataSource ID="ds_Inst" runat="server"
    ConnectionString="<%$ ConnectionStrings:SAPConfiguration.My.MySettings.SqlConn %>"
    SelectCommand="getInst" InsertCommand="insertInst"
        InsertCommandType="StoredProcedure" SelectCommandType="StoredProcedure"
        UpdateCommand="updateInst" UpdateCommandType="StoredProcedure"
        OldValuesParameterFormatString="original_{0}" EnableCaching="True"
        CacheDuration="600" FilterExpression="Num like '{0}'">
        <FilterParameters>
            <asp:ControlParameter ControlID="dl_filter_Inst" Name="Num"
                PropertyName="SelectedValue" DefaultValue="&quot;&quot;" />
            <asp:ControlParameter ControlID="tb_filter_Name" Name="Name"
                PropertyName="Text" />
            <asp:ControlParameter ControlID="rb_Enabled" Name="Enabled"
                PropertyName="Checked" />
            <asp:ControlParameter ControlID="rb_branchBilling" Name="BranchBilling"
                PropertyName="Checked" />
            <asp:ControlParameter ControlID="dl_filter_DB" Name="DB"
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="dl_MF" Name="MF"
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="dl_center" Name="Center"
                PropertyName="SelectedValue" />
        </FilterParameters>



Here is the gridview and column to be filtered:
XML
<asp:GridView ID="gv_Inst" runat="server" AutoGenerateColumns="False"
   DataSourceID="ds_Inst" AllowPaging="True" AllowSorting="True"
       AutoGenerateEditButton="True"
       PageSize="25" CssClass="gridview">
       <AlternatingRowStyle cssclass="altrow" />
   <Columns>
       <asp:BoundField DataField="Idx" HeaderText="Idx" SortExpression="Idx"
           InsertVisible="False" visible="false" >
       </asp:BoundField>
       <asp:BoundField DataField="Num" HeaderText="Number" SortExpression="Num"
           ReadOnly="False">
       <ControlStyle CssClass="controlClass" />
       </asp:BoundField>




http://msdn.microsoft.com/en-us/library/ms227680(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2[^]http://msdn.microsoft.com/en-us/library/ms227680(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2[^]
Posted
Comments
Member 10198178 3-Oct-13 17:52pm    
I tried adding the autopostback earlier and didn't help. I readded it and still didnt work.

I'm trying to follow these instructions and autopostback wasn't listed here:
http://msdn.microsoft.com/en-us/library/ms227680(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Thanks for the suggestion!

The DropDownList does not automatically post back when the selection changes.

Add AutoPostBack="True"

XML
<asp:DropDownList ID="dl_filter_Inst" runat="server" DataSourceID="ds_Inst"
                AutoPostBack="True" 
                DataTextField="Num" DataValueField="Num" width="98%"
                AppendDataBoundItems="true">
                <asp:ListItem Selected="True" Value="">Show All</asp:ListItem>
                </asp:DropDownList>
 
Share this answer
 
Ended up addiing variable to SPROC and using this code:

VB
Protected Sub btn_Apply_Click(sender As Object, e As EventArgs) Handles btn_Apply.Click
        gv_Inst.DataBind()
        btn_Clear.Enabled = True
    End Sub

    Protected Sub ds_Inst_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles ds_Inst.Selecting
        '<SelectParameters>
        '    <asp:Parameter Name="num" Type="String" />0
        '    <asp:Parameter Name="name" Type="String" />1
        '    <asp:Parameter Name="dbname" Type="String" />2
        '    <asp:Parameter Name="enable" Type="Boolean" />3
        '    <asp:Parameter Name="disable" Type="Boolean" />4
        '    <asp:Parameter Name="enableBranchBilling" Type="Boolean" />5
        '    <asp:Parameter Name="disableBranchBilling" Type="Boolean" />6
        '    <asp:Parameter Name="mainframe" Type="String" />7
        '    <asp:Parameter Name="center" Type="String" />8
        '  </SelectParameters>

        If cb_Inst.Checked Then
            e.Command.Parameters(0).Value = dl_filter_Inst.SelectedValue
        Else
            e.Command.Parameters(0).Value = "%"
        End If

        If cb_Name.Checked Then
            e.Command.Parameters(1).Value = tb_filter_Name.Text
        Else
            e.Command.Parameters(1).Value = "%"
        End If

        If cb_DB.Checked Then
            e.Command.Parameters(2).Value = dl_filter_DB.SelectedValue
        Else
            e.Command.Parameters(2).Value = "%"
        End If

        If cb_filter_Enable.Checked Then
            If rb_Enabled.Checked Then
                e.Command.Parameters(3).Value = True
                e.Command.Parameters(4).Value = True
            Else
                e.Command.Parameters(3).Value = False
                e.Command.Parameters(4).Value = False
            End If
        Else
            e.Command.Parameters(3).Value = True
            e.Command.Parameters(4).Value = False
        End If

        If cb_filter_BranchBilling.Checked Then
            If rb_branchBilling.Checked Then
                e.Command.Parameters(5).Value = True
                e.Command.Parameters(6).Value = True
            Else
                e.Command.Parameters(5).Value = False
                e.Command.Parameters(6).Value = False
            End If
        Else
            e.Command.Parameters(5).Value = True
            e.Command.Parameters(6).Value = False
        End If

        If cb_MF.Checked Then
            e.Command.Parameters(7).Value = dl_MF.SelectedValue
        Else
            e.Command.Parameters(7).Value = "%"
        End If

        If cb_Center.Checked Then
            e.Command.Parameters(8).Value = dl_center.SelectedValue
        Else
            e.Command.Parameters(8).Value = "%"
        End If
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900