Search code examples
asp.netgridviewcontrolparameter

How do I filter on 5 parameters, including a dropdown list using filter expression?


I'm trying to filter my gridview with 5 different parameters. I can get it to work fine but the issue arises when I add the "All" into the dropdownlist. If I select All in the dropdownlist I can't filter anymore. It will show the results for everything but filtering doesn't work. Is there something wrong in my filter expression or control parameter?

This is my current dropdownlist:

<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" 
DataTextField="Tab" DataValueField="Tab" AppendDataBoundItems="true" >
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="Tr">Tr</asp:ListItem>
<asp:ListItem Value="Out">Out</asp:ListItem>
<asp:ListItem Value="In">In</asp:ListItem>
</asp:DropDownList>

This is my source for the filtering:

<asp:SqlDataSource
SelectCommand="SELECT * FROM Log ORDER BY TimeStamp DESC" 
FilterExpression="Com LIKE '%{0}%' AND Usr LIKE '%{1}%' 
AND Tab = '{2}' AND TimeStamp >= '#{3}#' AND TimeStamp <= '#{4}#'">
<FilterParameters> 
<asp:ControlParameter ControlID="TB1" Name="Com" PropertyName="Text" Type="String" 
ConvertEmptyStringToNull="false" />

<asp:ControlParameter ControlID="TB2" Name="Usr" PropertyName="Text" Type="String" 
ConvertEmptyStringToNull="false" />   

<asp:ControlParameter Name="Tab" ControlID="DDL1" 
PropertyName="SelectedValue" ConvertEmptyStringToNull="true"/>

<asp:ControlParameter Name="Date" ControlID="DateFrom" Type="DateTime" PropertyName="Text"
ConvertEmptyStringToNull="false" />

<asp:ControlParameter Name="Date" ControlID="DateTo" Type="DateTime"   
PropertyName="Text" ConvertEmptyStringToNull="false" />
    </FilterParameters> </asp:SqlDataSource>

Solution

  • Update the Value of your ALL option in dropdown list

    <asp:ListItem Value="-1">All</asp:ListItem>
    

    Update your control parameter setting default value and removing ConvertEmptyStringToNull=false

    <asp:ControlParameter Name="Tab" ControlID="DDL1" 
    PropertyName="SelectedValue" Type="String" DefaultValue="-1"/>
    

    Update your filter expression for Tab

    FilterExpression="Com LIKE '%{0}%' AND Usr LIKE '%{1}%' 
    AND (Tab = '{2}' or '{2}' = '-1') AND TimeStamp >= '#{3}#' AND TimeStamp <= '#{4}#'">