Search code examples
asp.netfiltersqldatasource

Why does this FilterParameter not work as expected?


I have a gridview with columns: id, name, address, phone
And a TextBox: NameFilter

My gridview is populated by a SqlDataSource controller.

I need to filter my gridview registers with name.

I'm trying with 'FilterParameters' but I don't know why this does not work.

NameFilter

<asp:TextBox ID="NameFilter" runat="server" Text="1"></asp:TextBox>

SqlDataSource

    <asp:TextBox ID="NameFilter" runat="server"></asp:TextBox>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:okadConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:okadConnectionString.ProviderName %>" 
        SelectCommand="SELECT * FROM factory">
    </asp:SqlDataSource>

Solution

  • By using the FilterParameters you just create the SQL parameters, but to make them usable you need to include also the FilterExpression.

    for example you also need to add:
    FilterExpression="name='{0}'"

    reference: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterparameters.aspx

    Alternative you can add <SelectParameters> as

    <SelectParameters>
         <asp:ControlParameter ControlID="NameFilter" Name="name"
              PropertyName="Text" Type="String" />
    </SelectParameters>
    

    and use : SelectCommand = "SELECT * FROM factory WHERE name=@name"