[英]asp.net c# from server side lauch popup and wait for response before executing next line
[英]Wait popup response before C# function
我正在嘗試通過彈出窗口調用JavaScript函數,如果用戶單擊“確定”,它將調用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...
}
它與一個簡單的“確認”對話框一起工作。 但是我想自定義彈出窗口,這就是為什么我使用“ Alertify”庫的原因。
謝謝你的幫助。
更新(請參閱注釋3):通過單擊此鏈接( 從JavaScript調用函數背后的代碼(不是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");
當我單擊“確定”或“否”時,沒有任何反應。 提示的TextBox內容為:
函數(e,str){// str是輸入文本,如果(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 %>');
}
});
僅觸發“確定”。 取消按鈕不會顯示。
解決方案:使用了另一個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' });
而且有效!
解決方案:創建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/
您可以觸發對隱藏按鈕的單擊,您可以在其中的后面的代碼上附加單擊處理程序。
服務器:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.