Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Searching in GridView With Paging Enabled

Some time back I blogged about searching in the GridView control (http://aspadvice.com/blogs/azamsharp/archive/2006/10/02/Searching-in-the-GridView-Control.aspx). Many users asked me that how will the example work if the paging is enabled. Although, it may seems like a difficult task but it is all too simple. Check out the complete code listed below. The main idea is to assign the searchString (protected string variable) the value from the textbox before going to the other page. This will save the searchString across multiple pages of the GridView control.

 

HTML Code:

<div>

Enter Search String: <asp:TextBox ID="txtSearch" runat="server" />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />

<asp:GridView ID="GridView1" AllowPaging="true" PageSize="10" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging">

<Columns>

<asp:TemplateField HeaderText="Product Name">

<ItemTemplate>

<%# HighlightText(searchString, (string) Eval("ProductName")) %>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

C# Code Behind:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Text.RegularExpressions;

public partial class GridViewSearching : System.Web.UI.Page

{

protected string searchString = String.Empty;

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindData();

}

}

private void BindData()

{

string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT ProductID, ProductName FROM Products", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

protected string HighlightText(string searchWord, string inputText)

{

Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);

return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));

}

 

public string ReplaceKeywords(Match m)

{

return "<span class='highlight'>" + m.Value + "</span>";

}

protected void Button1_Click(object sender, EventArgs e)

{

searchString = txtSearch.Text;

BindData();

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

searchString = txtSearch.Text; // move the text in the searchString variable :)

BindData();

}

}

Now, the above will work for GridView with multiple pages.

 

Published Thursday, November 09, 2006 5:55 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# thank u @ Saturday, November 11, 2006 2:54 AM

hi again thank you for updating your blog! but if you could,describe this code and tell me how does if works.because your code didn't work in my system.I did every thing on this article but when I type somethimg is txtSearch it doesn't reflect. 2.If you could ,tell me about usage of Regex in this code . thank a lot Ali

peril_man

# re: Searching in GridView With Paging Enabled @ Saturday, November 11, 2006 10:04 AM

Hi,

I posted the complete code from start to finish. What problems are you having?

azamsharp

# Unlucky! @ Sunday, November 12, 2006 3:19 PM

hi azam I'm so sorry for disturbing but I don't know what is the problem? at least I copy all the code but it didn't worked...I do every thing as you wrote! I ran my page and the gridView appears but when I search for an item it doesn't reflect!

Ali

# Unlucky @ Sunday, November 12, 2006 3:20 PM

hi azam I'm so sorry for disturbing but I don't know what is the problem? at least I copy all the code but it didn't worked...I do every thing as you wrote! I ran my page and the gridView appears but when I search for an item it doesn't reflect!

ali

# re: Searching in GridView With Paging Enabled @ Monday, November 13, 2006 1:56 PM

Hi Azam, Thanks for putting up the code that can work with pagination. However, I do have a question. Let's say I have a gridview that shows results from database based on the user chosen parameters. On the screen, gridview is showing the data that is there on page 1. Now, if the user wants to do a search inside any one of the column in gridview and enters a string in the text box and hits search button, I want the gridview to show the respective page(may be page no.10 where the search string is highlighted within a column). Somehow the code that you have shared is not doing that or am I missing something? Can you please help us? Thanks, Anu

anu

# re: Searching in GridView With Paging Enabled @ Tuesday, November 14, 2006 12:44 PM

hi I wannna know is it a good idea to use DataView?I mean have a use dataview.find method? and after finiding the desired record open on the related page of gridview?

Ali

# re: Searching in GridView With Paging Enabled @ Tuesday, November 14, 2006 6:56 PM

Anu and Ali,

I think you can use the code that I provided with some extra code. The extra code will search in the DataSet or any container and depending on the row number and the number of rows displayed in GridView it will print out the page number using the DataSet and not the GridView control. The bottom line is search the DataSet and not the GridView.

azamsharp

# re: Searching in GridView With Paging Enabled @ Monday, November 27, 2006 12:09 PM

Hi Azam, Thank you for pointing us in the right direction. I have another question. let me know if it doesn't make any sense In VS2003, I have extended properties of Datagrid and compiled it as a separate class. In an .aspx page, this is how I was referencing the extended datagrid. <%@ Register TagPrefix="expo" Namespace="WReports" Assembly="WReports" %> and was accessing it like shown below I am not able to do the same thing in VS2005 using gridview. (<%@ Register TagPrefix="expo" Namespace="MultiSortGridView" Assembly="MultiSortGridView"%>). The register tagprefix doesn’t take the assembly at all. Do you have any idea how it can be done?

Anu

# re: Searching in GridView With Paging Enabled @ Wednesday, January 03, 2007 11:05 AM

Could you publish this in VB.net for those who works with vb.net. other wise, it is awsome trick. thanks for your hard work

yosef teferi

# re: Searching in GridView With Paging Enabled @ Wednesday, January 03, 2007 11:11 AM

Hi, You can check out the C# to Vb.NEt convertor. Here is the one that I like: http://www.kamalpatel.net/ConvertCSharp2VB.aspx

AzamSharp

# Searching in GridView REVISITED @ Thursday, January 04, 2007 9:44 PM

Some time back I posted about how you can search inside the GridView. You can find the post here. Many

AzamSharp

# re: Searching in GridView With Paging Enabled @ Thursday, March 22, 2007 2:06 PM

Hi: I have implemented your code and it works great. However, I have paging enabled and I want the user to search all pages. I am using objectdatasource. so when I seach for a value in page two while I am on page 1, how can I change the page so that the search goes and highlight the value on page two? Thank you

Rostam

# re: Searching in GridView With Paging Enabled @ Friday, April 20, 2007 6:34 PM

For everyone that is saying that the code is not working, look at the line: return "" + m.Value + ""; Change it to be return "" + m.Value + ""; It works fine. Just have to remember to read the code you are pasting and watch out for those class "gotchas"... HTH, bill

Bill

# re: Searching in GridView With Paging Enabled @ Wednesday, May 16, 2007 6:05 AM

Hello, Im using almost the same code as you but I get info from the previous Page aswell in the BindData() method, like this ContentPlaceHolder cont = (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("contentplaceholder1"); This works fine the first time the page loads, but when I try to page the grid it says : Object reference not set to an instance of an object. Any Ideas ?

Dreamer

# re: Searching in GridView With Paging Enabled @ Tuesday, July 10, 2007 2:31 AM

Dear, I dont know what is wrong....but it is not searchinh the Gridview items....I have copied ur whole code...but still it is not working ..please check if something is left???

Nitin Sharma

# re: Searching in GridView With Paging Enabled @ Thursday, September 20, 2007 1:06 PM

Good article, the author thanks!

北京租车

# re: Searching in GridView With Paging Enabled @ Monday, October 22, 2007 3:41 PM

Hello: Although you are maintaining the search string after posting back from paging, I think that some of us are asking how we can search all data in a gridview by traversing the pages w/o client intervention and multiple post backs to the server. To request the end user to page through the entire grid, even if you retain their search criteria, is a bit much. I will try to put together an example which would require one post back, the initial search request, and have the user "land" on the first row of whatever page the valid data is on. Thanks! John

John VandenBrook

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below