Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.57/5 (3 votes)
See more:
JavaScript
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/ASPSnippets_Pager.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
GetCustomers(1);
});
$("[id*=txtSearch]").live("keyup", function () {
GetCustomers(parseInt(1));
});
$(".Pager .page").live("click", function () {
GetCustomers(parseInt($(this).attr('page')));
});
function SearchTerm() {
return jQuery.trim($("[id*=txtSearch]").val());
};
function GetCustomers(pageIndex) {
$.ajax({
type: "POST",

url: "webform1.aspx/GetCustomers",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
var row;
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
if (row == null) {
row = $("[id*=gvCustomers] tr:last-child").clone(true);
}
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
if (customers.length > 0) {
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).html($(this).find("ContactName").text());
$("td", row).eq(1).html($(this).find("CustomerID").text());
$("td", row).eq(2).html($(this).find("City").text());
$("[id*=gvCustomers]").append(row);
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
var pager = xml.find("Pager");
$(".Pager").ASPSnippets_Pager({
ActiveCssClass: "current",
PagerCssClass: "pager",
PageIndex: parseInt(pager.find("PageIndex").text()),
PageSize: parseInt(pager.find("PageSize").text()),
RecordCount: parseInt(pager.find("RecordCount").text())
});
 
$(".ContactName").each(function () {
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "" + SearchTerm() + ""));
});

} else {
var empty_row = row.clone(true);
$("td:first-child", empty_row).attr("colspan", $("td", row).length);
$("td:first-child", empty_row).attr("align", "center");
$("td:first-child", empty_row).html("No records found for the search criteria.");
$("td", empty_row).not($("td:first-child", empty_row)).remove();
$("[id*=gvCustomers]").append(empty_row);
 

}
 
};
</script>

---------------------------------------------------------------
XML
Search:
<asp:TextBox ID="txtSearch" runat="server" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name"
            ItemStyle-CssClass="ContactName" />
        <asp:BoundField HeaderStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
        <asp:BoundField HeaderStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>
Posted
Updated 23-Sep-14 1:37am
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-14 0:58am    
Not a question. This is you who searches in one column. If you need to search in all columns, search in all columns. Also, you can consider searching in data source (if you have any) instead of UI.
—SA

1 solution

Simply change your query to add multiple columns. I think your example uses stored procedure to retrieve the value from the database so change the query in the stored procedure from

SQL
SELECT ROW_NUMBER() OVER(ORDER BY [CustomerID] ASC) AS RowNumber
      ,[CustomerID]
      ,[CompanyName]
      ,[ContactName]
      ,[City]
      INTO #Results
      FROM [Customers]
      WHERE [ContactName] LIKE @SearchTerm + '%' OR @SearchTerm = ''


SQL
SELECT ROW_NUMBER() OVER
      (
            ORDER BY [CustomerID] ASC
      )AS RowNumber
      ,[CustomerID]
      ,[CompanyName]
      ,[ContactName]
      ,[City]
      INTO #Results
      FROM [Customers]
      WHERE [ContactName] LIKE @SearchTerm + '%' OR [CustomerID] LIKE @SearchTerm + '%' OR [City] LIKE @SearchTerm + '%' OR @SearchTerm = ''


and replace the following line in your jquery from
JavaScript
$(".ContactName").each(function () {
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "" + SearchTerm() + ""));
});


TO

JavaScript
$("#<%=gvCustomers.ClientID%> tr:has(td)").children().each(function () {
var cellText = $(this).text().toLowerCase();
if (cellText.indexOf(SearchTerm().toLowerCase()) >= 0) //Check if data matches
{
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "" + SearchTerm() + ""));
}
});


Hope the above code changes help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900