使用ASP.NET中的jquery ajax在客户端使用复选框填充gridview [英] Populate gridview with checkbox on client side using jquery ajax in ASP.NET

查看:82
本文介绍了使用ASP.NET中的jquery ajax在客户端使用复选框填充gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用jquery与搜索和过滤器绑定gridview。但是它是绑定数据。但是当我向Gridview添加一个Checkbox字段时它没有绑定。可能有人帮忙我请。谢谢。



我尝试了什么:



Hi All,
I am binding a gridview using jquery with Search and Filter .However it is binding data.But when I add a Checkbox field to the Gridview it is not binding.Could someone help me please.Thanks in advance.

What I have tried:

<asp:GridView ID="gvCustomers" CssClass="mydatagrid" RowStyle-Height="50px" Width="100%" runat="server" AutoGenerateColumns="false">
         <Columns>
             <%--<asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="SlNo." 
              ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30px">
             <ItemTemplate>
            <%# Container.DataItemIndex + 1%>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" Width="3%" />
            <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>--%>
           <asp:BoundField HeaderStyle-Width="150px" DataField="IN005_01" HeaderText="CustomerID" />
        <asp:BoundField HeaderStyle-Width="150px" DataField="IN005_02" HeaderText="Contact Name"
         ItemStyle-CssClass="ContactName" />
       
         <asp:BoundField HeaderStyle-Width="150px" DataField="IN001_02" HeaderText="Category" />
              
         <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%">
          <HeaderTemplate>
          <asp:CheckBox ID="chk_All" runat="server" AutoPostBack="True" oncheckedchanged="chk_All_CheckedChanged"  />
          </HeaderTemplate>
           <ItemTemplate>
           <asp:CheckBox ID="chk_select" OnCheckedChanged="chk_select_CheckedChanged" AutoPostBack="True" runat="server"  />
           </ItemTemplate>
           <ItemStyle HorizontalAlign="Center" Width="3%" />
           </asp:TemplateField>
         </Columns>
         </asp:GridView>
















 <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: "SalaryAddition.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*=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("IN005_01").text());
                    $("td", row).eq(1).html($(this).find("IN005_02").text());
                    $("td", row).eq(2).html($(this).find("IN001_02").text());
                    $("td", row).find('[id*=chkStatus]').attr('checked', $(this).find("Status").text());
                   // $("td", row).find('[id*=chk_select]').attr('checked',$(this));
                   // $("td", row).eq(3).html($(this).find("chk_select").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())
                });

                $(".ContactName").each(function () {
                    var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
                    $(this).html($(this).text().replace(searchPattern, "" + SearchTerm() + ""));
                });
            } 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>













[WebMethod]
       public static string GetCustomers(string searchTerm, int pageIndex)
       {
           string query = "[SP006]";
           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["INSMCPConnectionString"].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("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;
                   }
               }
           }
       }

推荐答案

(功能(){
GetCustomers(1);
});
(function () { GetCustomers(1); });


([id * = txtSearch])。live(keyup,function() {
GetCustomers(parseInt(1));
});
("[id*=txtSearch]").live("keyup", function () { GetCustomers(parseInt(1)); });


(。Pager .page)。live(click,function(){
GetCustomers(parseInt(
(".Pager .page").live("click", function () { GetCustomers(parseInt(


这篇关于使用ASP.NET中的jquery ajax在客户端使用复选框填充gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆