Javascript 按ID删除用户不起作用,但按ID编辑用户起作用

Javascript 按ID删除用户不起作用,但按ID编辑用户起作用,javascript,c#,Javascript,C#,在我的管理面板中,我有一个用户列表,可以在网格中查看用户详细信息。在侧面,我有一个编辑按钮和一个删除按钮。“编辑”按钮工作正常。我可以编辑用户详细信息并更新,没有任何问题。问题是,当我删除用户时,它会删除gridview第一行中的用户,无论我选择删除哪个用户 这是我正在使用的完整代码: <script type="text/javascript"> var selectedPageSize = 10 /* Default size is 10. */

在我的管理面板中,我有一个用户列表,可以在网格中查看用户详细信息。在侧面,我有一个编辑按钮和一个删除按钮。“编辑”按钮工作正常。我可以编辑用户详细信息并更新,没有任何问题。问题是,当我删除用户时,它会删除gridview第一行中的用户,无论我选择删除哪个用户

这是我正在使用的完整代码:

<script type="text/javascript">

        var selectedPageSize = 10   /* Default size is 10. */
        var currentPageIndex = 1    /* Default pageindex is 1. */

        $(function () {
            GetUsers(currentPageIndex, selectedPageSize);
        });

        $(document).on("keyup", "[id*=txtSearch]", function () {
            GetUsers(currentPageIndex, selectedPageSize);
        });

        $(document).on("click", ".Pager .page", function () {
            currentPageIndex = parseInt($(this).attr('page'))
            GetUsers(currentPageIndex, selectedPageSize);
        });

        function changePagesize(obj) {
            selectedPageSize = $("[id*=ddlPageSize]").val();
            GetUsers(currentPageIndex, selectedPageSize);
        }

        function SearchKey() {
            return jQuery.trim($("[id*=txtSearch]").val());
        };
        function GetUsers(pageIndex, pageSize) {
            var gData = [];
            gData[0] = pageIndex;
            gData[1] = pageSize;
            gData[2] = SearchKey();
            var jsonData = JSON.stringify({ gData: gData });

            $.ajax({
                type: "POST",
                url: "users.aspx/GetUsers",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: "true",
                success: BindUsersList,
                failure: function (response) {
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }
        var row;
        function BindUsersList(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var users = xml.find("dsUsers");
            var html = '<table id="gvUsers">';
            if (row == null) {
                row = $("#<%=gvUsers.ClientID %> tr:last-child").clone(true);
            }
            $("#<%=gvUsers.ClientID %> tr").not($("#<%=gvUsers.ClientID %> tr:first-child")).remove();
            html += $("#<%=gvUsers.ClientID %>").html();
            if (users.length > 0) {
                $.each(users, function () {
                    html += "<tr>";

                    $(row).find("[id*=imgPreview]").attr("src", "../imageHandler.ashx?user_id_pk=" + $(this).find("user_id_pk").text() + "");
                    $(row).find("[id*=lblFullName]").html($(this).find("FullName").text());
                    $(row).find("[id*=lblEmail]").html($(this).find("email").text());
                    $(row).find("[id*=lblDob]").html($(this).find("dob").text());

                    if ($(this).find("user_status").text() == "true") {
                        $(row).find("[id*=lblStatus]").html("Active");
                        $(row).find("[id*=lblStatus]").attr("class", "label label-success label-transparent");
                    }
                    else {
                        $(row).find("[id*=lblStatus]").html("Inactive");
                        $(row).find("[id*=lblStatus]").attr("class", "label label-danger label-transparent");
                    }
                    if ($(this).find("accountStatus").text() == "locked") {
                        $(row).find("[id*=lblAccountStatus]").attr("class", "ti-lock text-danger");
                        $(row).find("[id*=lblAccountStatus]").attr("data-toggle", "tooltip");
                        $(row).find("[id*=lblAccountStatus]").attr("data-placement", "left");
                        $(row).find("[id*=lblAccountStatus]").attr("title", "Account locked due to invalid login attempts.");
                    }
                    else {
                        $(row).find("[id*=lblAccountStatus]").attr("class", "ti-unlock text-success");
                    }

                    $(row).find("[id*=lblRole]").html($(this).find("user_role").text());

                    $(row).find("[id*=aEditUserDetails]").attr("data-user-id", $(this).find("user_id_pk").text());
                    $(row).find("[id*=aEditUserDetails]").attr("title", "Edit User Details");

                    $(row).find("[id*=aEditUserDetails]").attr("data-user-id", $(this).find("user_id_pk").text());
                    $(row).find("[id*=aDeleteUserModal]").attr("data-user-id", $(this).find("user_id_pk").text());

                    $("#<%=gvUsers.ClientID %>").append(row);
                    row = $("#<%=gvUsers.ClientID %> tr:last-child").clone(true);

                    html += $(row).html();
                    html += "</tr>";
                });
                var pager = xml.find("dtPager");
                $("[id*=divPager]").gvPager({
                    ActiveCssClass: "current",
                    PagerCssClass: "pager",
                    PageIndex: parseInt(pager.find("PageIndex").text()),
                    PageSize: parseInt(pager.find("PageSize").text()),
                    RecordCount: parseInt(pager.find("RecordCount").text())
                });
                $(".cssSearch").each(function () {
                    var searchPattern = new RegExp('(' + SearchKey() + ')', 'ig');
                    $(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchKey() + "</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 Data Available.");
                $("td", empty_row).not($("td:first-child", empty_row)).remove();
                $("#<%=gvUsers.ClientID %>").append(empty_row);
            }
            html += '</table>';
        };

        /* Edit user details. */
        $("[id*=aEditUser]").click(function () {
            $.ajax({
                type: "POST",
                url: "users.aspx/EditUser",
                data: '{userId: ' + $(this).data('user-id') + '}',
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                async: "true",
                success: function (response) {
                    location.href = "newuser.aspx?uid=" + response.d;
                },
                error: function (response) {
                    Notification(response.status + ' ' + response.statusText, 'Error', 'error');
                }
            });
        });

        /* Delete user. */
        $("[id*=aDeleteUserModal]").click(function () {

                $.ajax({
                    type: "POST",
                    url: "users.aspx/DeleteUser",
                    data: '{userId: ' + $(this).data('user-id') + '}',
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    async: "true",
                    success: function (response) {
                        if (response.d == 'DeletePermissionDenied') {
                            Notification('You dont have the permission to delete the data, contact your administrator.', 'Permission Denied', 'error');
                        }
                        else if (response.d == 'Success') {
                            GetUsers(currentPageIndex, selectedPageSize);
                            Notification('User deleted successfully.', 'Success', 'success');
                        }
                    },
                    error: function (response) {
                        Notification(response.status + ' ' + response.statusText, 'Error', 'error');
                    }
                });
            //}
        });
    </script>

}

如果您的id已发送。检查您的后端,因为我看不出这里有错误。您还设置了两次aEditUserDetails的id。您还应该使用
.on('click'…
而不是
。单击(…
),因为您使用ajax更改DOM,而第二次删除由于侦听器不好而无法工作。您好,谢谢,当我使用.on时(“单击”…它会从列表中删除用户。我已删除了第二行aEditUserDetails。但是,如果您的ID是在请求中发送的,并且如果您处理该值,则仍然无法在后端进行luckcheck。我想您的问题可能在后端。我已使用隐藏的代码更新了我的问题
public static string DeleteUser(int userId)
{
    string result = "";
    if (isDeletePermission.Equals(false))
    {
        result = "DeletePermissionDenied";
    }
    else
    {
        string strQuery = "EXEC sp_um_deleteUser " + userId + "," + int.Parse(HttpContext.Current.Session["UserId"].ToString()) + "";
        bool isResult = DataControl.ExecuteNonQuery(strQuery);
        if (isResult.Equals(true))
        {
            result = "Success";
        }
        else
        {
            result = "Error";
        }
    }
    return result;
}