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

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.
2
tmpuzer
3/14/2008 9:46:13 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
7423 Views

Similar Articles

[PageSpeed] 24
Get it on Google Play
Get it on Apple App Store

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.
4
tmpuzer
3/14/2008 10:33:03 PM

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.
1
tmpuzer
3/18/2008 8:16:47 PM

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>
 

1
bryantlee5311
3/20/2008 2:24:11 AM

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 

 

0
tawammar
6/14/2008 5:53:07 AM
Reply:

Similar Artilces:

Gridview Search using dropdownlist, textbox, search button.
 I can only search in gridview using a textbox and a search button. What i want to do is. I have a dropdownlist where the items are the columns of the employee table, a textbox where a user input a value. And when the user click the search button it will be displayed on the gridview. something like this. Select * from "Employee" where "dropdownlist_sample" is LIKE "textbox_sample" Here are the columns of my Employee Table EmployeeID, LastName, FirstName, Address, ContactNo, Birthdate, DateHired,  and Empoyee Status. and bt...

How Can I Search Using DropDownList, TextBox, Gridview and a Search Button?
I have a DropDownList with two items:  "Customer" and "Vendor". I like user to select, for example, "Customer", from the DropDownList then in a TextBox, enter the customer name. Clicking the Search button, would return the appropriate data to the GridView-- depending on what has been selected in the DropDownList and what was entered in the TextBox. Is this possible?  Your help would be very much appreciated. Jim   I am trying the same thing, it’s surprising you haven’t received any reply’s by now. Seems like a common task. I am going to try a cross p...

How to search a gridview with sqldatasource dynamically using multiple search terms from a single textbox control
Could someone please let me know how I can let people search a gridview (with paging and sorting) using a single textbox that accepts multiple search terms? I also want to be able to edit and delete a row from the filtered search results. I want to be able to apply the search criteria to each column in my gridview. For example, a user might enter a search string: "jack jones" Say I have a gridview that has three columns: id, firstname, lastname with the following data: 1 fred flintstone2 wilma flintstone3 jack beanstalk4 jones jack5 jack jones6 hungry jack I want the gridview ...

filtered search with textbox and dropdownlist
 hi,just wondering how i would go about making a search facility?1. textbox1 = search term2. dropdownlist = column name (firstname, surname, etc) 3. button = submit4. display in gridview my sql isnt that good and im totally new to asp.net. any help would be great   in above design.. user would be able to search on just ONE field only.. as you can select only one field from dropdownlist and can write search term for that field in textbox1...what i would prefer is.. make a search page with all possible search  parameters selection with DropDownList / ListBo...

Filtering/Search control for Gridview
Hi All,  can anybody suggest me some ready-made control (Free) or simple way to build one for searching/filtering in gridview. For example, I have 100's products rows in Gridview from database, i want to create a functionality where user can click on search buttons like (A, B, C,D,,,,,) and based on Alphabet, the Gridview filter the rows only. Thanks,   Here's one example for you: ASPX <%@ page autoeventwireup="true" codefile="FilterWithAlphabetLinkButtons.aspx.cs" inherits="GridView_FilterWithAlphabetLinkButtons" language="...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Gridview Search Without Object Datasource Filter ParametersGridview Search Without Object Datasource Filter Parameters
Couple weeks ago I read a article about highlighted search in Gridview  (http://blog.evonet.com.au/post/2008/06/25/Gridview-with-highlighted-search-results.aspx) and in my project I used things I learned but now things changed I have a gridview and in code I give its datasource so because I dont use any objectdatasource I dont know where to write my filtering parameters. Is there anyway to use filtering without objectdatasource, or is there a way to give objectdatasource an source then bind it to my gridview. My code:  //No objectdatasource how can I conduct a search?GridView1.Da...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Textbox's searches Filtered Gridview
This feels like an easy answer but I can't seem to figure it out.  I have a gridview from a SQL datasource linked to a dropdown menu from another SQL source.   The dropdown menu basically filters/populates the gridview according to what I have selected.  I also have 2 textbox's that I want to use to search the gridview SQL datasource, but the search needs to be linked to the selected dropdown item.  So if I have Product ID "10" selected in the dropdown list, I want either textbox to only search this selected ID number. I'm very new to asp.net and don't understand scr...

keyword search not working (GridView controlled by TextBox)
Hi there, I need help here. All I want to do is to search the keyword in the Oracle database. What I used is to add a text box and a GridView. The textbox is to take input and in the GridView, I define to select the entries where the TITLE has the keyword matched. (I used the build-in query function to define the CONTROL to be LIKE. the SQL syntax is can be found in the below code and it was auto generated according to the query defintion) <div> Type a keyword to search <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server"...

How to use a DropDownList to search into Gridview which has NULL values
Hi All, I've a GridView whit the following values: ID | User | Group | Location 1 | user1 | Windows | home 2 | user2  | Linux | office 3 | user3 | Windows | NULL Then, I've a DropDrownList whit the following values: home office I Would like that when I open the page, all records are displayed, even "user3" He's value in the "Location" field is "NULL" Then, when I choose "home" from the DropDrownList, I must see only "user1" instead, when I choose "office" from the DropDrownList, I must see ...

Advanced search, listbox filters, search results in gridView? (design problem sort of)
 Hi.  I'd like to write an advanced search form... I'm using VB.net, ASP.NET 2.0, VS 2005 (also asp.net ajax) I'd have many filters, mostly multiple select listBoxes. On the right-hand side of each of the filter there would be a button "Add to filter". User would select filters and add them to "current filter" list or possibly *client-side generated gridView (* <- is that possible?). Finally a user would then click on a "Search" button and a gridViewSearchResults would be displayed using dataSource with all those filters.I would also lik...

Search Engine based on the user input into textbox, dropdownlist, and gridview with checkboxlist
Hello all, I have two tables with sample data as following: tblStudentID  | School | Name | Age | Year====================================1   | A      | Jane |  5  | 20062   | B      | Jo   |  4  | 20083   | C      | Al   |  1  | 20074   | A      | Ron  |  5  | 20085   | B      | Tom  |  5  | 20066   | A   &n...

GridView Filtering and Searching
Hello Everyone, i'm having a gridview wich list clients details. i have setup a search text box and a dropdownlist for filtering. Everything is working except when i try to navigate the next page....i'm getting the filter results and not the search ones....i have created a Hidden control and store the search value... i'm still having problem with this. Anyone have a solution or run into similar situation? Cheers You will need to store the filtered selectcommand in Session and then reuse this saved selectcommand when postback occurs or the page will default back to you fir...

Search gridview control
I am using the ASP.NET 2.0 with C#.  I want to add a dropdown list with all the names of my Gridview columns to be the items in the dropdown list. Then I want my users to select one of those column name to search by and enter a value inside a textbox. Exmaple: User selects "Name" to search by and enter "John" in the textbox.  When the user clicks on the Go button all results with "John" should be listed in the Gridview alone.  Any help would be greatly appreciated.  Don't forget to click "Mark as Answer" on the post that helped...

Gridview Search by Textbox
Here is the situation.  I can use a single textbox as the search control for any columns in my Gridview, but when I try to make the textbox as the control to search for name and taxid number I get an error saying "Error converting data type nvarchar to numeric. "  Is there something I am missing that keeps a single textbox from searching for both numbers and letters?   can you post your code here and the exact stack trace?Liming XuJumptree ASP.NET 2.0 Project Management - With Source Code, Free to Non Profit Org and Effectively Manage Projects/Tasks/...

Filtering Gridview using other gridview results
Hi there, I'm trying to filter a gridview using 5 other gridviews.  These 5 gridviews all have username columns.  I would like to find only the usernames that appeared in every single gridview. example: GV1: oliver - John - Pete GV2: John - Alex - whatever GV3:John - blah - blah2 GV4: Blah - Pete - John GV5: Alex - whatever - John FinalGV: John   How do I do this.....its too much for my head right now!! Thanks in advance! This project is due very soon and i really need to figure this out.....any ideas?...

Populate Label, Dropdownlist, Textbox and Gridview from a SQL DataBase using VB.NET
 Good Day, Please help me, i need a VB.NET code for my web appz, i already know how to connect to a sql server database using this codes Dim conn As New Data.OleDb.OleDbConnection conn = New Data.OleDb.OleDbConnection("Provider=SQLOLEDB.1;Password=******;Persist Security Info=True;User ID=***;Initial Catalog=Database;Data Source=ServerName") conn.Open() Dim cmd As Data.OleDb.OleDbCommand cmd = New Data.OleDb.OleDbCommand("Select Field1 From Table1", conn)So i would like to ask how can i bind this VB.NET connection code to a Label and/or Dropdownlist and...

Cannot get a RowFilter in a GridView control to work with a DateTime data type using a Dropdownlist as the filtering method
Hello,  I have a DataView that I am using as the data source for a GridView control and when I add a DropDownList control into the header of any column type other than a DateTime type the filtering works fine on Postback from the dropdown.  I just cannot get it to work with a DateTime type.  Here is a little bit of the code: //Here's all the things I have tried:  //Value is a string representation of my DateTime type (i.e. "12/01/2007 12:04:00 AM")filter = propName + "=#" + Convert.ToDateTime(value) + "#"; filter = propName + "...

Creating a Search Function to search for a row in Gridview
Hi all,  i've encountered some problems while trying to create the search function. I am able to search at the first try, however at the 2nd time i click the search button, i am unable to carry out the search function. It seems to postback the previous search. I also would like to be able to just search through the dropdownlist alone to filter out. Please help out here. thanks! here are the codes.   <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:DropDownList ID="DropDownList2" runat="server" DataSo...

search engine and displaying search results with gridview
 Hi all,I am a newbie with ASP.NET.I have a gridview that display data from the database.I want to implement a search engine. When the user enters a keyword in the textbox then click on the search button the result will be displayedto the gridview. How do I do this? How do I implement a search engine so that when the user enters a keyword on the textbox and hit the search button the data will then be displayed in the gridview? Here's is my code. The texbox and search button are highlighted in bold case: <%@ Page Language="C#" AutoEventWireup="true"&n...

Gridview sorting, gridview searching and image autoresizing
Hi,    I am relatively new to asp.net and am developing an application but have a few questions which I am struggling with.1. Is there a way of auto-resizing an image uploaded by a user for display?2. What do I need to do to enable sorting on an objectdatasource - do I need to change the SelectPortfolios stored procedure and what changes should I make?3. How do I apply a search feature within the app which will allow the user to search for a particular portfolio (searching the gridview and displaying results in the gridview) - also does the fact that I am using an objectdataso...

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one colum.now my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the data.now my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Web resources about - Gridview Search: use DropDownList and TextBox to search / filter gridview? - asp.net.presentation-controls

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 2/16/2016 8:48:56 PM