Hi makumbi,
Please refre below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Default
<div>
<table cellpadding="2" class="newStyle1">
<tr>
<td>Student Search</td>
<td class="auto-style1">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:HiddenField ID="hfId" runat="server" />
<asp:HiddenField ID="hfName" runat="server" />
<asp:HiddenField ID="hfCity" runat="server" />
<asp:HiddenField ID="hfCountry" runat="server" />
<asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:ButtonField CommandName="Select" HeaderText="Select" Text="Select" ControlStyle-CssClass="selectedRow" />
</Columns>
</asp:GridView>
<br />
<div class="Pager"></div>
</td>
</tr>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="ASPSnippets_Pager.min.js"></script>
<script type="text/javascript">
$(function () {
GetCustomers(1);
$("body").on("keyup", "[id*=txtSearch]", function () {
GetCustomers(parseInt(1));
});
$("body").on("click", ".Pager .page", function () {
GetCustomers(parseInt($(this).attr('page')));
});
$("body").on("click", ".selectedRow", function () {
var id = $(this).closest('tr').find('td').eq(0).html();
var name = $(this).closest('tr').find('td').eq(1).html();
var city = $(this).closest('tr').find('td').eq(2).html();
var country = $(this).closest('tr').find('td').eq(3).html();
$('[id$=hfId]').val(id);
$('[id$=hfName]').val(name);
$('[id$=hfCity]').val(city);
$('[id$=hfCountry]').val(country);
});
});
function SearchTerm() {
return jQuery.trim($("[id*=txtSearch]").val());
};
function GetCustomers(pageIndex) {
$.ajax({
type: "POST",
url: "Default.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*=StudentGrid] tr:last-child").clone(true);
}
$("[id*=StudentGrid] tr").not($("[id*=StudentGrid] tr:first-child")).remove();
if (customers.length > 0) {
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).html($(this).find("CustomerID").text());
$("td", row).eq(1).html($(this).find("ContactName").text());
$("td", row).eq(2).html($(this).find("City").text());
$("td", row).eq(3).html($(this).find("Country").text());
$("[id*=StudentGrid]").append(row);
row = $("[id*=StudentGrid] 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())
});
$(".Name").each(function () {
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
});
} 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*=StudentGrid]").append(empty_row);
}
};
</script>
Home
<table>
<tr>
<td>Id:</td>
<td><asp:Label ID="lblId" runat="server" /></td>
</tr>
<tr>
<td>Name:</td>
<td><asp:Label ID="lblName" runat="server" /></td>
</tr>
<tr>
<td>City:</td>
<td><asp:Label ID="lblCity" runat="server" /></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:Label ID="lblCountry" runat="server" /></td>
</tr>
</table>
Namespaces
Imports System.Data
Imports System.Data.SqlClient
Code
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDummyRow()
End If
End Sub
Private Sub BindDummyRow()
Dim dummy As DataTable = New DataTable()
dummy.Columns.Add("CustomerId")
dummy.Columns.Add("ContactName")
dummy.Columns.Add("City")
dummy.Columns.Add("Country")
dummy.Rows.Add()
StudentGrid.DataSource = dummy
StudentGrid.DataBind()
End Sub
<System.Web.Services.WebMethod()>
Public Shared Function GetCustomers(ByVal searchTerm As String, ByVal pageIndex As Integer) As String
Dim query As String = "[GetCustomersPageWiseSearch]"
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm)
cmd.Parameters.AddWithValue("@PageIndex", pageIndex)
cmd.Parameters.AddWithValue("@PageSize", 10)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output
Return GetData(cmd, pageIndex).GetXml()
End Function
Private Shared Function GetData(ByVal cmd As SqlCommand, ByVal pageIndex As Integer) As DataSet
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(strConnString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
sda.Fill(ds, "Customers")
Dim dt As DataTable = New DataTable("Pager")
dt.Columns.Add("PageIndex")
dt.Columns.Add("PageSize")
dt.Columns.Add("RecordCount")
dt.Rows.Add()
dt.Rows(0)("PageIndex") = pageIndex
dt.Rows(0)("PageSize") = 10
dt.Rows(0)("RecordCount") = cmd.Parameters("@RecordCount").Value
ds.Tables.Add(dt)
Return ds
End Using
End Using
End Using
End Function
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim id As String = hfId.Value
Dim name As String = hfName.Value
Dim city As String = hfCity.Value
Dim country As String = hfCountry.Value
Session("ID") = id
Session("Name") = name
Session("City") = city
Session("Country") = country
Response.Redirect("Home.aspx")
End Sub
Home
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
lblId.Text = Session("ID").ToString()
lblName.Text = Session("Name").ToString()
lblCity.Text = Session("City").ToString()
lblCountry.Text = Session("Country").ToString()
End Sub
Sreceenshot