在 C# 函数之前等待弹出响应
Wait popup response before C# function
我正在尝试使用弹出窗口调用 JavaScript 函数,如果用户单击 "Ok",它会调用 C# 函数。但是页面总是在我加载 JavaScript 函数的同时回发。
我的 HTML ASP:Button :
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClick="PrchBtn_Click" OnClientClick = "Confirm();" />
OnClientClick,它调用这个JS函数:
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
});
}
然后是我的 C# 函数:
public void PrchBtn_Click(object sender, EventArgs e)
{
//Code here...
}
它使用一个简单的 "confirm" 对话框。但我想自定义弹出窗口,这就是我使用 "Alertify" 库的原因。
感谢您的帮助。
更新(见评论#3):
按照这个 link (Call Code Behind Function from JavaScript (not AJAX!))
这是我的实际代码:
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
} else {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}
});
}
但问题是一样的,JS和C#同时做事。
更新(奖励错误):
我不知道为什么,但我的 alertify 被窃听了。
根据提示:
alertify.prompt("Message", function (e, str) {
// str is the input text
if (e) {
Console.Log("Ok");
} else {
Console.Log("No");
}
}, "Default Value");
当我点击“确定”或“否”时,没有触发任何东西。提示的文本框内容为:
function (e, str) { // str is the input text if (e) { Console.Log("Ok"); } else { Console.Log("No"); } }
还有 Alertify.Confirm
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
} else {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}
});
只有 "Ok" 在开火。取消按钮没有任何作用。
解决方案:
又拿了一个 alertify.js (http://alertifyjs.com/)
这是我的 JS 函数:
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
},
function () {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}).set('labels', { ok: 'Ok', cancel: 'No' });
而且有效!
您可以在隐藏按钮上触发点击,您可以在隐藏按钮上附加点击处理程序。
服务器:
protected void Page_Load(object sender, EventArgs e)
{
PrchBtnHiddenNo.Click += PrchBtnHiddenNo_Click;
PrchBtnHiddenYes.Click += PrchBtnHiddenYes_Click;
}
void PrchBtnHiddenYes_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void PrchBtnHiddenNo_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
客户:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<script src="Scripts/alertify.js"></script>
<link href="Content/alertify.default.css" rel="stylesheet" />
<link href="Content/alertify.core.css" rel="stylesheet" />
<hgroup class="title">
<h1>Test alertify</h1>
</hgroup>
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="Click Here" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHiddenYes" ClientIDMode="Static" Style="display: none;" />
<asp:Button runat="server" ID="PrchBtnHiddenNo" ClientIDMode="Static" Style="display: none;" />
<script>
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('Do it', function (e) {
if (e) {
$('#PrchBtnHiddenYes').click();
confirm_value.value = "Yes";
} else {
$('#PrchBtnHiddenNo').click();
confirm_value.value = "No";
}
});
}
</script>
</asp:Content>
JS 源代码和 css 下载自:http://fabien-d.github.io/alertify.js/
重要提示:如果你试图触发点击文件上传输入 IE 不会让你这样做。
您可以使用纯 java 脚本没有 jquery:
document.getElementById("PrchBtnHiddenYes").click();
解法:
创建 2 个 HTML 按钮,一个可见链接到 JavaScript 函数,另一个不可见链接到 C# 方法:
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />
JS :
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
},
function () {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}).set('labels', { ok: 'Ok', cancel: 'No' });
}
当你点击第一个按钮时,它会调用JS。然后JS会调用第二个按钮的PostBack。
我在 Alertify 上遇到了问题,所以我使用了另一个来源:http://alertifyjs.com/
我正在尝试使用弹出窗口调用 JavaScript 函数,如果用户单击 "Ok",它会调用 C# 函数。但是页面总是在我加载 JavaScript 函数的同时回发。
我的 HTML ASP:Button :
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClick="PrchBtn_Click" OnClientClick = "Confirm();" />
OnClientClick,它调用这个JS函数:
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
});
}
然后是我的 C# 函数:
public void PrchBtn_Click(object sender, EventArgs e)
{
//Code here...
}
它使用一个简单的 "confirm" 对话框。但我想自定义弹出窗口,这就是我使用 "Alertify" 库的原因。
感谢您的帮助。
更新(见评论#3): 按照这个 link (Call Code Behind Function from JavaScript (not AJAX!)) 这是我的实际代码:
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
} else {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}
});
}
但问题是一样的,JS和C#同时做事。
更新(奖励错误): 我不知道为什么,但我的 alertify 被窃听了。 根据提示:
alertify.prompt("Message", function (e, str) {
// str is the input text
if (e) {
Console.Log("Ok");
} else {
Console.Log("No");
}
}, "Default Value");
当我点击“确定”或“否”时,没有触发任何东西。提示的文本框内容为:
function (e, str) { // str is the input text if (e) { Console.Log("Ok"); } else { Console.Log("No"); } }
还有 Alertify.Confirm
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
if (e) {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
} else {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}
});
只有 "Ok" 在开火。取消按钮没有任何作用。
解决方案: 又拿了一个 alertify.js (http://alertifyjs.com/) 这是我的 JS 函数:
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
},
function () {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}).set('labels', { ok: 'Ok', cancel: 'No' });
而且有效!
您可以在隐藏按钮上触发点击,您可以在隐藏按钮上附加点击处理程序。
服务器:
protected void Page_Load(object sender, EventArgs e)
{
PrchBtnHiddenNo.Click += PrchBtnHiddenNo_Click;
PrchBtnHiddenYes.Click += PrchBtnHiddenYes_Click;
}
void PrchBtnHiddenYes_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void PrchBtnHiddenNo_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
客户:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<script src="Scripts/alertify.js"></script>
<link href="Content/alertify.default.css" rel="stylesheet" />
<link href="Content/alertify.core.css" rel="stylesheet" />
<hgroup class="title">
<h1>Test alertify</h1>
</hgroup>
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="Click Here" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHiddenYes" ClientIDMode="Static" Style="display: none;" />
<asp:Button runat="server" ID="PrchBtnHiddenNo" ClientIDMode="Static" Style="display: none;" />
<script>
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('Do it', function (e) {
if (e) {
$('#PrchBtnHiddenYes').click();
confirm_value.value = "Yes";
} else {
$('#PrchBtnHiddenNo').click();
confirm_value.value = "No";
}
});
}
</script>
</asp:Content>
JS 源代码和 css 下载自:http://fabien-d.github.io/alertify.js/
重要提示:如果你试图触发点击文件上传输入 IE 不会让你这样做。
您可以使用纯 java 脚本没有 jquery:
document.getElementById("PrchBtnHiddenYes").click();
解法: 创建 2 个 HTML 按钮,一个可见链接到 JavaScript 函数,另一个不可见链接到 C# 方法:
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />
JS :
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
confirm_value.value = "Yes";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
},
function () {
confirm_value.value = "No";
document.forms[0].appendChild(confirm_value);
__doPostBack('<%= PrchBtnHidden.UniqueID %>');
}).set('labels', { ok: 'Ok', cancel: 'No' });
}
当你点击第一个按钮时,它会调用JS。然后JS会调用第二个按钮的PostBack。 我在 Alertify 上遇到了问题,所以我使用了另一个来源:http://alertifyjs.com/