Search and filter does not returning any data in ASP.Net using jQuery Ajax
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="../Scripts/ASPSnippets_Pager.min.js"></script>
<script type="text/javascript">
$(function () {
GetCustomers(1);
});
$("[id*=txtSearch]").on("keyup", function () {
GetCustomers(parseInt(1));
});
$(".Pager .page").on("click", function () {
GetCustomers(parseInt($(this).attr('page')));
});
function SearchTerm() {
return jQuery.trim($("[id*=txtSearch]").val());
};
function GetCustomers(pageIndex) {
$.ajax({
type: "POST",
url: "SearchStudent.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(1).html($(this).find("Admno").text());
$("td", row).eq(2).html($(this).find("Name").text());
$("td", row).eq(3).html($(this).find("Class").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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit Record" Width="156px" Height="34px" />
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
</td>
<td> </td>
<td rowspan="2"> </td>
</tr>
<tr>
<td colspan="5">
<asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False" ItemStyle-Width="Auto" CellSpacing="1" ClientIDMode="Static">
<Columns>
<asp:CommandField ShowHeader="True" ShowSelectButton="True" />
<asp:BoundField DataField="admno" HeaderText="Admno" SortExpression="account" />
<asp:BoundField DataField="Name" HeaderText="Student Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Class">
<ItemTemplate>
<asp:DropDownList ID="ddlCLASS" runat="server" Height="25px" Width="74px">
</asp:DropDownList>
<asp:Label ID="lblCategory2" runat="server" Text='<%# Eval("Class") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stream">
<ItemTemplate>
<asp:DropDownList ID="ddlStream" runat="server" Height="24px" Width="98px" OnSelectedIndexChanged="ddlStream_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:Label ID="lblCategory3" runat="server" Text='<%# Eval("Stream") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Type">
<ItemTemplate>
<asp:DropDownList ID="ddlSTDTYPE" runat="server" Height="26px" Width="87px">
</asp:DropDownList>
<asp:Label ID="lblCategory" runat="server" Text='<%# Eval("STDTYPE") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:DropDownList ID="ddlSex" runat="server">
</asp:DropDownList>
<asp:Label ID="lblCategory4" runat="server" Text='<%# Eval("Sex") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="House" HeaderText="Colour" SortExpression="House" />
<asp:BoundField DataField="ACNOS" HeaderText="ACNOS" SortExpression="ACNOS" />
<asp:ButtonField CommandName="PayDetails" HeaderText="View Payment Details" Text="PayDetails" />
<asp:ButtonField CommandName="Stream Progress" HeaderText="Stream Progress" Text="StreamProgress" />
<asp:TemplateField HeaderText="Rclass">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rclass") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Rclass" runat="server" Text='<%# Eval("Rclass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td colspan="6"> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
<System.Web.Services.WebMethod()>
Public Shared Function GetCustomers(searchTerm As String, pageIndex As Integer) As String
Dim query As String = "[GetCustomers_Pager2000]"
Dim cmd As New SqlCommand(query)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm)
cmd.Parameters.AddWithValue("@PageIndex", pageIndex)
cmd.Parameters.AddWithValue("@PageSize", PageSize)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output
Return GetData(cmd, pageIndex).GetXml()
End Function
Private Shared Function GetData(cmd As SqlCommand, pageIndex As Integer) As DataSet
Dim strConnString As String = ConfigurationManager.ConnectionStrings("UNIFORMConnectionString").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("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") = PageSize
dt.Rows(0)("RecordCount") = cmd.Parameters("@RecordCount").Value
ds.Tables.Add(dt)
Return ds
End Using
End Using
End Using
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Dim cmd As New SqlCommand("SELECT NAME, HOUSE, CLASS, STREAM, SEX, ADMNO, ACCOUNT, STATUS, STDTYPE,ACNOS,Rclass FROM Student")
'' StudentGrid.DataSource = Me.ExecuteQuery(cmd, "SELECT")
' StudentGrid.DataBind()
'Required for jQuery DataTables to work.
' StudentGrid.UseAccessibleHeader = True
' StudentGrid.HeaderRow.TableSection = TableRowSection.TableHeader
BindDummyRow()
End If
End Sub
Private Sub BindDummyRow()
Dim dummy As New DataTable()
dummy.Columns.Add("Admno")
dummy.Columns.Add("Name")
dummy.Columns.Add("Class")
dummy.Columns.Add("Stream")
dummy.Columns.Add("Sex")
dummy.Columns.Add("STDTYPE")
dummy.Columns.Add("House")
dummy.Columns.Add("ACNOS")
dummy.Columns.Add("RClass")
dummy.Rows.Add()
StudentGrid.DataSource = dummy
StudentGrid.DataBind()
End Sub
PROCEDURE [dbo].[GetCustomers_Pager2000]
@PageIndex INT = 1
,@PageSize INT = 50
,@RecordCount INT OUTPUT,
@SearchTerm nvarchar(200)
AS
BEGIN
SET NOCOUNT ON;
SELECT IDENTITY(INT,1,1) AS RowNumber
,[Account]
,[Name]
,[Class]
,[Stream]
,[House]
,[STDTYPE]
,[ACNOS]
,[SEX]
,[Rclass]
INTO #Results
FROM [STUDENT]
WHERE ([Name] LIKE @SearchTerm or [Name] LIKE '%'+@SearchTerm+'%' or [Name] LIKE '%' + @SearchTerm or [Name] LIKE @SearchTerm + '%') OR @SearchTerm = '' OR @SearchTerm = ''
SELECT @RecordCount = COUNT(*)
FROM #Results
SELECT * FROM #Results
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
END