Hi micah,
I have created a sample that full fill your requirement.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#dvCustomers").attr("style", "display:none");
$('[id*=txtSearch]').on("keyup", function () {
$.ajax({
type: "POST",
url: "CS.aspx/GetCustomers",
data: '{search:"' + $(this).val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) { alert(response.d); },
error: function (response) { alert(response.d); }
});
if ($(this).val() != '') {
$("#dvCustomers").attr("style", "display:block");
}
else {
$("#dvCustomers").attr("style", "display:none");
}
});
});
function OnSuccess(response) {
var table = "<table cellpadding='2' cellspacing='0' border='1' Style='width: 300px; height: 100px; border: dashed 2px #04AFEF; background-color: #B0E2F5'>";
if (response.d != '') {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
customers.each(function () {
var customer = $(this);
var contactName = customer.find("ContactName").text();
var customerID = customer.find("CustomerID").text();
var companyName = customer.find("CompanyName").text();
table += "<tr><td><b>Contact Name:</b>" + contactName + "<br/><b>Customer ID:</b>" + customerID + "<br/><b>Company Name:</b>" + companyName + "</td></tr>";
});
table += "</table>";
}
else {
table += "<tr align='center'><td><b>No Result For the Criteria</b></td></tr>";
}
$("#dvCustomers").html(table);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox runat="server" ID="txtSearch" />
<br />
<div id="dvCustomers">
<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table" Style="width: 300px;
height: 100px; border: dashed 2px #04AFEF; background-color: #B0E2F5">
<ItemTemplate>
<b>Contact Name:</b><span class="contactName">
<%# Eval("ContactName")%></span><br />
<b>Customer ID:</b><span class="customerID">
<%# Eval("CustomerID")%></span><br />
<b>Company Name:</b> <span class="companyName">
<%# Eval("CompanyName")%></span><br />
<hr />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//dlCustomers.DataSource = GetCustomersData("");
//dlCustomers.DataBind();
}
}
public static DataSet GetCustomersData(string search)
{
string query = "[GetCustomersPageWiseSearch]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", 1);
cmd.Parameters.AddWithValue("@PageSize", 100);
cmd.Parameters.AddWithValue("@search", search);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd);
}
private static DataSet GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "Customers");
//DataTable dt = new DataTable("PageCount");
//dt.Columns.Add("PageCount");
//dt.Rows.Add();
//dt.Rows[0][0] = cmd.Parameters["@RecordCount"].Value;
//ds.Tables.Add(dt);
return ds;
}
}
}
}
[System.Web.Services.WebMethod]
public static string GetCustomers(string search)
{
DataSet ds = GetCustomersData(search);
if (ds.Tables[0].Rows.Count > 0)
{
return GetCustomersData(search).GetXml();
}
else
{
return "";
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'dlCustomers.DataSource = GetCustomersData("")
'dlCustomers.DataBind()
End If
End Sub
Public Shared Function GetCustomersData(search As String) As DataSet
Dim query As String = "[GetCustomersPageWiseSearch]"
Dim cmd As New SqlCommand(query)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PageIndex", 1)
cmd.Parameters.AddWithValue("@PageSize", 100)
cmd.Parameters.AddWithValue("@search", search)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output
Return GetData(cmd)
End Function
Private Shared Function GetData(cmd As SqlCommand) As DataSet
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds, "Customers")
'Dim dt As New DataTable("PageCount")
'dt.Columns.Add("PageCount")
'dt.Rows.Add()
'dt.Rows(0)(0) = cmd.Parameters("@RecordCount").Value
'ds.Tables.Add(dt)
Return ds
End Using
End Using
End Using
End Function
<System.Web.Services.WebMethod()> _
Public Shared Function GetCustomers(search As String) As String
Dim ds As DataSet = GetCustomersData(search)
If ds.Tables(0).Rows.Count > 0 Then
Return GetCustomersData(search).GetXml()
Else
Return ""
End If
End Function
SQL
CREATE PROCEDURE [dbo].[GetCustomersPageWiseSearch]
@PageIndex INT = 1
,@PageSize INT = 5
,@RecordCount INT OUTPUT
,@search nvarchar(100) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT ROW_NUMBER() OVER
(
ORDER BY [CustomerID] desc
)AS RowNumber
,[CustomerID]
,[CompanyName]
,[ContactName]
,(SELECT TOP 1 ShipCity FROM Orders WHERE CustomerId = Customers.CustomerId) as ShipCity
,(SELECT TOP 1 OrderID FROM ORDERS WHERE CustomerId = Customers.CustomerID) as OrderId
INTO #Results
FROM [Customers]
WHERE (ContactName LIKE @search + '%' OR @search IS NULL)
SELECT @RecordCount = COUNT(*) FROM #Results
SELECT * FROM #Results
DROP TABLE #Results
END
Screenshot