Gridview Search: use DropDownList and TextBox to search / filter gridview?

Last post 06-14-2008 1:53 AM by tawammar. 4 replies.

Sort Posts:

  • Gridview Search: use DropDownList and TextBox to search / filter gridview?

    03-14-2008, 5:46 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

    Let's say I have a gridview with three columns: First Name, Last Name, ID Number.  Here's an example of what I want to do:

    I want to display all the records in the gridview to start.  There would be a dropdownlist that lets the user select whether to search by first name, last name, or ID number and a text box that lets them type in what they want to search for (and a button to start the search).  After searching, the gridview would only display the records that matched the criteria.

    The gridview needs to be editable, sortable, and pageable even after searching/filtering.  I'm using an SqlDataSource for the time being.  I already know the basics of how to create the gridview and make it editable, sortable, and pageable.  I have experimented with modifying the SelectCommand of the data source with mixed results.  When I edit the SelectCommand and rebind the datasource, attempting to edit a record throws an "Invalid postback or callback argument"

    How can I do this?  Or if you know a tutorial of this that would be fantastic.
     

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: Gridview Search: use DropDownList and TextBox to search / filter gridview?

    03-14-2008, 6:33 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

    I think I found an elegant solution to this.  The key seems to be to use a FilterExpression.  Like so:

     <asp:DropDownList ID="SearchByDropDownList" runat="server">
    <asp:ListItem Text="First Name" Value="first_name" />
    <asp:ListItem Text="Last Name" Value="last_name" />
    <asp:ListItem Text="ID Number" Value="some_id" />
    </asp:DropDownList>

    <asp:TextBox ID="SearchTextBox" runat="server"></asp:TextBox>
    <asp:Button ID="SearchButton" runat="server" Text="Search" OnClick="SearchButton_Click" />
    <asp:Button ID="ClearButton" runat="server" Text="Clear" OnClick="ClearButton_Click" />

    ... snip ...

    <asp:SqlDataSource ID="PersonDataSource" runat="server" SelectCommand="SELECT first_name, last_name, some_id FROM people ORDER BY last_name DESC, first_name DESC" FilterExpression="{0} LIKE '%{1}%'" ConnectionString="<%$ ConnectionStrings:SqlServerConnection %>">

    <FilterParameters>
    <asp:ControlParameter ControlID="SearchByDropDownList" Name="FieldToSearch" PropertyName="SelectedValue" />
    <asp:ControlParameter ControlID="SearchTextBox" Name="SearchCriteria" />
    </FilterParameters>
      
    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: Gridview Search: use DropDownList and TextBox to search / filter gridview?

    03-18-2008, 4:16 PM
    • Loading...
    • tmpuzer
    • Joined on 03-07-2008, 5:45 PM
    • Posts 315

    Although I like the solution above, I learned that the FilterExpression is not executed against the database but is instead run on the dataset.  I'd prefer to have the WHERE clause run against the database.  Any ideas of how I can do this maybe with dynamic SelectCommands? 

    If a post helps me I'll always eventually mark it as an answer. But I frequently don't mark it right away because I feel once a thread is marked as answered, discussion tends to end. And I like to discuss things a bit.
  • Re: Gridview Search: use DropDownList and TextBox to search / filter gridview?

    03-19-2008, 10:24 PM
    Answer

    hey buddy~i need do those dynamic searching like your case~so below is my coding which work after i refer to coding in tis post..and i did edit a little bit to make it work ..and it work fine for me then..hope its can help u ya~hehe~

     

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body background="App_Themes/Theme2/Vitrox11_thumb [Desktop Resolution].bmp" bgproperties="fixed">
        <form id="form1" runat="server">
        <div>
            &nbsp;
            <hr style="z-index: 104; left: 0px; position: absolute; top: 0px" />
            <hr style="z-index: 105; left: 0px; position: absolute; top: 0px" />
            &nbsp; &nbsp; &nbsp;
        </div>
            &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;
            <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 139px; position: absolute;
                top: 162px" AutoPostBack="True"></asp:TextBox>
            &nbsp; &nbsp;
            <asp:Button ID="Button1" runat="server" Style="z-index: 101; left: 277px; position: absolute;
                top: 207px" Text="Button" />
            &nbsp;
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT EmployeeName, EmployeeID, Position, Department, Extension, Email, HomePhone, HandPhone FROM Employee WHERE (EmployeeName LIKE '%' + @EmployeeName + '%') OR (EmployeeID LIKE N'%' + @EmployeeID + N'%') OR (Department LIKE '%' + @Department + '%') OR (Extension LIKE '%' + @Extension + '%') OR (Email LIKE '%' + @Email + '%')" FilterExpression="{0} LIKE '%{1}%'">
               <FilterParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="FieldToSearch" PropertyName="SelectedValue" />
                    <asp:ControlParameter ControlID="TextBox1" Name="Search Criteria" PropertyName="Text" />
                </FilterParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox1" Name="EmployeeName" PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox1" Name="EmployeeID" PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox1" Name="Department" PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox1" Name="Extension" PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox1" Name="Email" PropertyName="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeName"
                DataSourceID="SqlDataSource1" Style="z-index: 102; left: 52px; position: absolute;
                top: 332px">
                <Columns>
                    <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" ReadOnly="True"
                        SortExpression="EmployeeName" />
                    <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                    <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" />
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                    <asp:BoundField DataField="Extension" HeaderText="Extension" SortExpression="Extension" />
                    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                    <asp:BoundField DataField="HomePhone" HeaderText="HomePhone" SortExpression="HomePhone" />
                    <asp:BoundField DataField="HandPhone" HeaderText="HandPhone" SortExpression="HandPhone" />
                </Columns>
            </asp:GridView>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Style="z-index: 106;
                left: 316px; position: absolute; top: 165px" Width="158px">
                <asp:ListItem>EmployeeName</asp:ListItem>
                <asp:ListItem>EmployeeID</asp:ListItem>
                <asp:ListItem>Extension</asp:ListItem>
                <asp:ListItem>Department</asp:ListItem>
                <asp:ListItem>Email</asp:ListItem>
            </asp:DropDownList>
           
        </form>
    </body>
    </html>
     

  • Re: Gridview Search: use DropDownList and TextBox to search / filter gridview?

    06-14-2008, 1:53 AM
    • Loading...
    • tawammar
    • Joined on 07-22-2006, 5:58 AM
    • Posts 4

    You should find what you are looking for at http://tawatech.blogspot.com/2008/06/aspnet-datagridview-ajax-data-filter.html

    Download the flash video to see how to use the control. and you can find the source code overthere 2

     

    regards,

     

    tawammar 

     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter