我有个问题。我正在尝试从列表中删除某些内容,但它从不从 .aspx.cs 调用我的删除函数
这是我的 JavaScript 代码:
function doTheDelete(doIDeleteExpenses) {
if (selectedExpensesList.length > 0) {
$.ajax({
type: "POST",
//url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
url: '<%=ResolveUrl("~/Expenses/ViewExpenses.aspx/deleteSelectedExpense")%>',
data: "{'DeleteExpenses' : " + "'" + doIDeleteExpenses + " '}",
//dataaaaaa
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var ss = data.d;
if (ss.length > 0) {
for (var i = 0; i < ss.length; ++i) {
$.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true });
alert(ss[i]);
}
}
$("#viewTasksGrid").flexReload();
},
error: function (data) {
$.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
alert('Error Deleting Expense');
if (window.console) {
console.log(data);
}
}
});
} else {
showMessage('No expenses are selected.');
}
}
function getSelectedExpenseIDs() {
var selectedExpensesList = new Array;
var i = 0;
$('.expenseCheckBox:checked').each(function () {
if ($(this)[0].id !== "checkAllExpenses") {
selectedExpensesList[i] = $(this)[0].id.split('_')[1];
++i;
}
});
return selectedExpensesList;
}
这是我在 aspx.cs 中永远不会被调用的方法:
[WebMethod]
public static string[] deleteSelectedExpense(bool DeleteExpenses, String[] ExpID)
{
var rList = new List<string>();
//var canDeleteTasks = false;
//var canDeleteTrackers = false;
var canDeleteExpenses = false;
var investigatorID = (int)HttpContext.Current.Session["InvestigatorID"];
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
var cmd = new SqlCommand("p_Admin_Permissions_CanDeleteExpenses", conn);
cmd.Parameters.Add(new SqlParameter("@InvestigatorID", SqlDbType.Int));
cmd.Parameters["@InvestigatorID"].Value = investigatorID;
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
canDeleteExpenses = (bool)cmd.ExecuteScalar();
}
catch (SqlException sql)
{
if (!rList.Contains("Can not connect to the database. Please try again."))
rList.Add("Can not connect to the database. Please try again.");
}
catch (Exception ex)
{
if (!rList.Contains("An Error Occured"))
rList.Add("An Error Occured");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
if (canDeleteExpenses)
{
foreach (var expense in ExpID)
{
if (canDeleteExpenses && DeleteExpenses)
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
cmd = new SqlCommand("p_DeleteExpenses", conn);
cmd.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int));
cmd.Parameters["@ExpID"].Value = int.Parse(expense);
cmd.Parameters.Add("@Message", SqlDbType.NVarChar, 50);
cmd.Parameters["@Message"].Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException sql)
{
if (!rList.Contains("Error Connecting to the Database. Unable To Delete Expense(s)."))
rList.Add("Error Connecting to the Database. Unable To Delete Expense(s).");
}
catch (Exception ex)
{
if (!rList.Contains("An Error Occured"))
rList.Add("An Error Occured");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
else if (!canDeleteExpenses && DeleteExpenses)
{
rList.Add("You do not have permission to delete Expenses");
}
else
{
if (!rList.Contains("You do not have permission to delete the task(s)."))
rList.Add("You do not have permission to delete the task(s).");
}
}
}
//var serializer = new JavaScriptSerializer();
//var re = serializer.Serialize(rList.ToArray());
return rList.ToArray();
}
当我在浏览器控制台中时,它告诉我一个内部错误(500)。好吧,原因很明显。