We help IT Professionals succeed at work.

GridVeiw Search Functionality with textbox and dropdown control

SirReadAlot
SirReadAlot asked
on
Hi experts

follow this example,

how do i add a textbox to help me implement a through search on the grid?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
    <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
        <form id="form1" runat="server">

            <p>Show all employees with the following title:
            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                AutoPostBack="True">
                <asp:ListItem Selected="True">Sales Representative</asp:ListItem>
                <asp:ListItem>Sales Manager</asp:ListItem>
                <asp:ListItem>Vice President, Sales</asp:ListItem>
            </asp:DropDownList></p>

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
                FilterExpression="Title='{0}'">
                <FilterParameters>
                    <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
                </FilterParameters>
            </asp:SqlDataSource>

            <p><asp:GridView
                id="GridView1"
                runat="server"
                DataSourceID="SqlDataSource1"
                AutoGenerateColumns="False">
                <columns>
                    <asp:BoundField Visible="False" DataField="EmployeeID" />
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                </columns>
            </asp:GridView></p>

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

thanks
Comment
Watch Question

Entrepeneur
Commented:
Hi, just add a new control parameter:
<asp:TextBox id="TextBox1" runat="server" />
 
<asp:SqlDataSource
  id="SqlDataSource1"
  runat="server"
  ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
  SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
  FilterExpression="Title='{0}' OR FirstName LIKE '%{1}%'">
  <FilterParameters>
    <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
    <asp:ControlParameter Name="Name" ControlId="TextBox1" PropertyName="Text"/>
  </FilterParameters>
</asp:SqlDataSource>

Open in new window

Author

Commented:
Hi

do i have to do more than the above, when I insert a name in the text box, it does not return the right data

thanks

Author

Commented:
fixed it
Oliver AmayaEntrepeneur

Commented:
Well depends on how you want to look, I use a Like that can search any part of the FirstName for the input of the textbox.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.