Hello,
I am refering this link for Search and Filter data using type in textbox:-
http://www.aspsnippets.com/Articles/Search-and-Filter-GridView-as-you-type-in-TextBox-using-jQuery-AJAX-in-ASPNet.aspx#comments
but its not running in my case becasue i have on status column(Checkbox field data) and Edit,update and delete also.
So, how can we retrive checkbox column data in gridview acoordoing to boolean value.
Please help me.
Thanks in Advance.
Ankit Agarwal
<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: "CS.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("DealerDetails");
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("Status").text());
$("td", row).eq(1).html($(this).find("DealerCode").text());
$("td", row).eq(2).html($(this).find("DealerName").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())
});
$(".DealerName").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*=gvCustomers]").append(empty_row);
}
};
</script>
<asp:TextBox ID="txtSearch" runat="server" />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" CellPadding="2"
ForeColor="#333333" GridLines="None" CssClass="someClass" DataKeyNames="DealerLoginName"
Width="100%" AllowPaging="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Status" HeaderStyle-BorderColor="black" HeaderStyle-BorderWidth="1px"
HeaderStyle-Width="10%" HeaderStyle-BorderStyle="Solid">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" AutoPostBack="true" Enabled="true" Checked='<%# Convert.ToBoolean(Eval("Status")) %>'
Text='<%# Eval("Status").ToString().Equals("True") ? " Approved " : " Pending " %>'
OnCheckedChanged="chkStatus_CheckedChanged" />
</ItemTemplate>
<FooterStyle Width="10%" />
<HeaderStyle BorderColor="Black" BorderWidth="1px" BorderStyle="Solid" HorizontalAlign="Center"
VerticalAlign="Middle" Width="10%"></HeaderStyle>
<ItemStyle Font-Size="Small" Width="10%" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="DealerCode" HeaderText="Dealer Code" HeaderStyle-BorderColor="black"
HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid">
<FooterStyle Width="10%" />
<HeaderStyle BorderColor="Black" BorderWidth="1px" BorderStyle="Solid" Width="10%"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Width="10%" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="DealerName" HeaderText="Dealer Name" HeaderStyle-BorderColor="black"
HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid" ItemStyle-CssClass="DealerName">
<FooterStyle Width="10%" />
<HeaderStyle BorderColor="Black" BorderWidth="1px" BorderStyle="Solid" HorizontalAlign="Center"
VerticalAlign="Middle" Width="10%"></HeaderStyle>
<ItemStyle Width="10%" HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Width="10%" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Small"
Width="10%" HorizontalAlign="Center" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" Font-Size="Small" Width="10%" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add(new DataColumn("Status", typeof(bool)));
dummy.Columns.Add("SNo");
dummy.Columns.Add("DealerCode");
dummy.Columns.Add("DealerName");
dummy.Rows.Add();
gvCustomers.DataSource = dummy;
gvCustomers.DataBind();
}
[WebMethod]
public static string GetCustomers(string searchTerm, int pageIndex)
{
string query = "[procFindDealerDetails]";
SqlCommand cmd = 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();
}
private static DataSet GetData(SqlCommand cmd, int pageIndex)
{
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
con.Open();
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "DealerDetails");
DataTable dt = 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;
}
}
con.Close();
}
}