按下选择时,网格视图选择所有行

时间:2015-07-15 03:33:02

标签: javascript jquery asp.net gridview webmethod

我有gvCustomers,它在运行时使用webMethod和jquery填充。当我在命令字段中单击选择按钮时,将选中所有行。 我非常感谢你的帮助。

这是我的javascript文件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
        $(function () {
            if ($('#ContentPlaceHolder1_txtBuscador').val() == '') {
                GetCustomers(1);
            } else {
                GetCustomersBuscar(1);
            }
        });


        $("body").on("click", "#ContentPlaceHolder1_buscar", function () {

            GetCustomersBuscar(1);
        });
        function GetCustomers(pageIndex) {
            $.ajax({
                type: "POST",
                url: "Clientes.aspx/GetCustomers",
                data: '{pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }
        function GetCustomersBuscar(pageIndex) {
            $.ajax({
                type: "POST",
                url: "Clientes.aspx/GetCustomersBuscar",
                data: '{pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert($('#txtBuscContentPlaceHolder1_txtBuscadorador').val() + response.d);
                },
                error: function (response) {
                    console.log(data.error);
                    alert($('#ContentPlaceHolder1_txtBuscador').val() + 'Error' + response);
                }
            });
        }

        function OnSuccess(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Customers");
            var row = $("[id*=ContentPlaceHolder1_gvCustomers] tr:last-child").clone(true);
            $("[id*=ContentPlaceHolder1_gvCustomers] tr").not($("[id*=ContentPlaceHolder1_gvCustomers] tr:first-child")).remove();
            $.each(customers, function () {
                var customer = $(this);
                $("td", row).eq(1).html($(this).find("id").text());
                $("td", row).eq(2).html($(this).find("nombre").text());
                $("td", row).eq(3).html($(this).find("apellido").text());
                $("td", row).eq(4).html($(this).find("telefono").text());
                $("td", row).eq(5).html($(this).find("celular").text());
                $("td", row).eq(6).html($(this).find("dni").text());
                $("[id*=ContentPlaceHolder1_gvCustomers]").append(row);
                row = $("[id*=ContentPlaceHolder1_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())
            });
        };
    </script>

标记

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
      <asp:GridView ID="gvCustomers" runat="server" CellPadding="1" CssClass="table table-striped table-hover" GridLines="None" ShowHeaderWhenEmpty="True" ForeColor="#333333" AutoGenerateColumns="False" DataKeyNames="id">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" SelectText="Seleccionar" />
            <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" InsertVisible="False" ReadOnly="True" Visible="false" />
            <asp:BoundField DataField="nombre" HeaderText="Nombre" SortExpression="nombre" />
            <asp:BoundField DataField="apellido" HeaderText="Apellido" SortExpression="apellido" />
            <asp:BoundField DataField="telefono" HeaderText="Telefono" SortExpression="telefono" />
            <asp:BoundField DataField="celular" HeaderText="Celular" SortExpression="celular" />
            <asp:BoundField DataField="dni" HeaderText="DNI" SortExpression="dni" />
        </Columns>
    </asp:GridView>
</asp:Content>

代码隐藏

[WebMethod]
public static string GetCustomers(int pageIndex)
{
    string query = "[GetCustomers_Pager]";
    SqlCommand cmd = new SqlCommand(query);
    cmd.CommandType = CommandType.StoredProcedure;
    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();
}

0 个答案:

没有答案