在用户单击“删除”按钮后,我想从aspx页面的js函数访问cs函数。并将ID发送给CS函数。
我尝试以下代码:
js
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "Top demand apps-SOURCE CLARIFICATION"
$mail.HTMLBody="<html><head></head><body><b>Joseph</b></body></Html>"
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
cs
function deleteClick(ID) {
'<%Session["ID"] = "' + ID + '"; %>';
var x = '<%= Session["ID"] %>';
alert(x);
$.ajax({
type: "POST",
url: "AddPI.aspx/deleteClick",
//data: ' {ID:"' + ID + '"}',
data: JSON.stringify({ ID: "' + ID + '" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Success");
},
failure: function (response) {
alert("Error");
}
});
}
问题是无法传递ID!
编辑: 我通过将cs函数更改为static并编辑以下字段来解决该问题:
public void deleteClick(String ID)
{
Session["ID"] = Request.Params["ID"];
var id = "";
id = Session["ID"].ToString();
DialogResult myResult;
myResult = MessageBox.Show("Are you sure?", "Delete Confirmation",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (myResult == DialogResult.OK)
{
errorbox.Visible = false;
//connect to database and delete
}
}
就是这样。
现在的问题是如何在静态CS函数中更改aspx div的可见性?
data: "{ ID: '" + ID + "'}",
我得到了:非静态字段需要Ann对象引用。...
我尝试成功调用js
errorbox.Visible = false;
和js:
success: setVisibility,
但它不起作用!
答案 0 :(得分:1)
//Js Code
function deleteClick(ID) {
alert(ID);
$.ajax({
type: "POST",
url: "Default.aspx/deleteClick",
data: '{ID: "'+ID+'" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (response,a,a2) {
alert("Not Done");
}
});
}
//c# Code
[System.Web.Services.WebMethod]
public static string deleteClick(string ID)
{
//Your Code
return null;
}
答案 1 :(得分:0)
请注意,您的第一个问题已由您解决,太好了! :)
,您还有另一个问题,需要在发生错误时更改div的可见性。好吧,您将div errorbox
的可见性更改为true
作为默认值
errorbox.Visible = true;
在您的脚本部分,使用jquery隐藏该div:
$(function(){
$('[id$=errorbox]').hide(); //this will auto-run after page is completely loaded.
});
对您的cs函数进行一些更改,例如:
public string deleteClick(String ID)
{
Session["ID"] = Request.Params["ID"];
var id = "";
id = Session["ID"].ToString();
DialogResult myResult;
myResult = MessageBox.Show("Are you sure?", "Delete Confirmation",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (myResult == DialogResult.OK)
{
errorbox.Visible = false;
//connect to database and delete
return "true"; //if successful
}
return "false"; //if not successful
}
然后如果您的ajax响应发生任何错误(成功/错误),只需在ajax中执行此操作即可:
success: function (response) {
if(response.d == "true")
{
alert('success');
}
else{
$('[id$=errorbox]').show();
}
},
error: function (response) {
$('[id$=errorbox]').show();
}