Apologies beforehand, this is my first time coding using javascript. All of this I have written based off of other questions I found on here.
事先道歉,这是我第一次使用javascript编码。所有这些都是基于我在这里找到的其他问题编写的。
I have a call to a webmethod that checks whether a page is dirty and returns a boolean:
我调用了一个webmethod来检查页面是否脏并返回一个布尔值:
<script type="text/javascript">
function OnConfirm() {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "UserManagement.aspx/IsDirty",
dataType: "json",
data: '{}',
success: function OnDirtySuccess(result) {
if (result.d.toString() == "true") {
return confirm("You have unsaved changes, select OK to discard these changes without saving.");
}
else {
return false;
}
return true;
},
failure: function (response) {
return true;
}
});
}
</script>
I have a button that calls the script:
我有一个调用脚本的按钮:
<asp:Button ID="btnAddNewUser" CausesValidation="false" runat="server" Text="Add New User" OnClick="btnAddNewUser_Click" OnClientClick="return OnConfirm();" Width="140px" />
The method call is working correctly, I only see the confirm dialog when the current user has been modified. My issue is when I click 'cancel' the event for btnAddNewUser_Click is still firing.
方法调用工作正常,我只在修改当前用户时才看到确认对话框。我的问题是,当我点击'取消'时,btnAddNewUser_Click的事件仍在触发。
A different approach based on another question posted here, that did not work for me:
基于此处发布的另一个问题的不同方法对我不起作用:
<script type="text/javascript">
function OnConfirm() {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "UserManagement.aspx/IsDirty",
dataType: "json",
data: '{}',
success: function OnDirtySuccess(result) {
if (result.d.toString() == "true") {
if (!confirm("You have unsaved changes, select OK to discard these changes without saving."))
return false;
//return confirm("You have unsaved changes, select OK to discard these changes without saving.");
}
else {
return false;
}
return true;
},
failure: function (response) {
return true;
}
});
}
</script>
<asp:Button ID="btnAddNewUser" CausesValidation="false" runat="server" Text="Add New User" OnClick="btnAddNewUser_Click" OnClientClick="if (! OnConfirm()) return false;" Width="140px" />
Here, I modified the OnClientClick to if (! OnConfirm()) return false; and I tried using both versions of the confirm code inside OnConfirm(). However, when the confirm dialog pops up and I hit cancel, the OnClick event still fires.
在这里,我将OnClientClick修改为if(!OnConfirm())返回false;我尝试在OnConfirm()中使用两个版本的确认代码。但是,当弹出确认对话框并且我点击取消时,OnClick事件仍然会触发。
I've tried changing the OnClientClick function to (just to simplify the code and test):
我已经尝试将OnClientClick函数更改为(只是为了简化代码和测试):
<script type="text/javascript">
function OnConfirm() {
return confirm('Test ok/cancel?');
}
</script>
and the Ok/Cancel of the confirmation dialog is working properly.
和确认对话框的确定/取消正常工作。
What am I missing with my OnConfirm function?
我的OnConfirm功能缺少什么?
1 个解决方案
#1
0
Check this link .It has exactly the same answer of your question
检查此链接。它与您的问题的答案完全相同
Update for you.
为您更新。
Use POST to pass the data
使用POST传递数据
<asp:Button ID="btnAddNewUser" ClientIDMode="Static" CausesValidation="false" runat="server" Text="Add New User" Width="140px" />
and
<script type="text/javascript">
$(function() {
$('#btnAddNewUser').click(function(e) {
e.preventDefault();
//Make your ajax call
var params = {};
params.parameter = "passing string data";
$.ajax({
type: "POST",
url: "YourPageName.aspx/MyFunction1",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(params),
success: function(res) {
alert(res.d);
},
error: function(res) {
}
});
});
});
</script>
and YourPageName.aspx.cs file
和YourPageName.aspx.cs文件
[WebMethod]
public static string MyFunction1(string parameter)
{
return parameter;
}
#1
0
Check this link .It has exactly the same answer of your question
检查此链接。它与您的问题的答案完全相同
Update for you.
为您更新。
Use POST to pass the data
使用POST传递数据
<asp:Button ID="btnAddNewUser" ClientIDMode="Static" CausesValidation="false" runat="server" Text="Add New User" Width="140px" />
and
<script type="text/javascript">
$(function() {
$('#btnAddNewUser').click(function(e) {
e.preventDefault();
//Make your ajax call
var params = {};
params.parameter = "passing string data";
$.ajax({
type: "POST",
url: "YourPageName.aspx/MyFunction1",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(params),
success: function(res) {
alert(res.d);
},
error: function(res) {
}
});
});
});
</script>
and YourPageName.aspx.cs file
和YourPageName.aspx.cs文件
[WebMethod]
public static string MyFunction1(string parameter)
{
return parameter;
}