javascript - 按 ID 删除用户不起作用,但按 ID 编辑用户有效
问题描述
在我的管理面板中,我有一个用户列表,我可以在网格中查看用户详细信息。在侧面,我有一个编辑按钮和一个删除按钮。编辑按钮工作正常。我可以编辑用户详细信息并毫无问题地更新。问题是当我删除用户时,它会删除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>
谢谢。
编辑
代码背后
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;
}
}
解决方案
推荐阅读
- python - Python Kolmogorov-Smirnov (KS) 测试结果不一致
- ssl - 多个站点和多个 SSL 证书的 IIS10 SSL 配置
- ruby-on-rails - 如何防止 Ruby 以 gem 的类而不是我的类为目标?
- reactjs - react-i18next:尝试导入错误:“withNamespaces”未从“react-i18next”导出
- php - Symfony:驱动程序发生异常:找不到驱动程序
- r - R 快速傅里叶变换 (FFT) 滤波器
- bootstrap-select - bootstrap-select 在 safari 上拉伸,但在 chrome 上看起来很正常
- r - 如何编写动态网址代码以进行网页抓取?
- mysql - “where 子句”中的未知列在程序中失败
- python - 深度学习程序的多任务并发