Please help i have this code which works well it search student using the on keypress. my problem is making the returned data in the gridview editable so that i can modify it and save in the database after search.
if you take a keen look at this link below which i used to work on my example it returns the data well on keypress but the data returned is not editable yet in my situation i would want to search and edit at the same time hope I am clear this time round.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../Scripts/jquery-1.8.3.min.js"></script>
<script src="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: "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(2).html($(this).find("ADMNO").text());
$("td", row).eq(3).html($(this).find("Name").text());
$("td", row).eq(4).html($(this).find("Class").text());
$("td", row).eq(5).html($(this).find("Stream").text());
$("td", row).eq(6).html($(this).find("STDTYPE").text());
$("td", row).eq(7).html($(this).find("Sex").text());
$("td", row).eq(8).html($(this).find("House").text());
$("td", row).eq(9).html($(this).find("ACNOS").text());
$("td", row).eq(10).html($(this).find("Rclass").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" />
Search:
<asp:TextBox ID="txtSearch" runat="server" />
</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:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowHeader="True" ShowSelectButton="True" />
<asp:TemplateField HeaderText="Admno" SortExpression="ADMNO">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ADMNO") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ADMNO") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("admno") %>' Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:TextBox ID="Names" runat="server" Text='<%# Eval("Name") %>' Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<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:TemplateField HeaderText="Colour" SortExpression="House">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("House") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("House") %>'></asp:Label>
<asp:TextBox ID="House" runat="server" Text='<%# Eval("House") %>' Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ACNOS" SortExpression="ACNOS">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ACNOS") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ACNOS") %>'></asp:Label>
<asp:TextBox ID="ACNOS" runat="server" Text='<%# Eval("ACNOS") %>' Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<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>
<asp:TextBox ID="Rclasse" runat="server" Text='<%# Eval("Rclass") %>' Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="PayDetails" HeaderText="View Payment Details" Text="PayDetails" />
<asp:ButtonField CommandName="Stream Progress" HeaderText="Stream Progress" Text="StreamProgress" />
</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