I'm trying to call a JavaScript function with a popup, and if user click on "Ok", it calls a C# function. But the page always PostBack in same time I am loading the JavaScript function.
My HTML ASP:Button :
<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClick="PrchBtn_Click" OnClientClick = "Confirm();" />
OnClientClick, it calls this JS function :
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);
});
}
And then my C# function :
public void PrchBtn_Click(object sender, EventArgs e)
{
//Code here...
}
It's working with a simple "confirm" dialog. But I want to custom the popup that's why I use "Alertify" library.
Thanks for your help.
UPDATE (See comments #3) : By following this link (Call Code Behind Function from JavaScript (not AJAX!)) This is my actual code :
<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 %>');
}
});
}
But the problem is the same, JS and C# are doing stuff at the same time.
UPDATE (Bonus bug) : I don't know why but my alertify is bugged. On a prompt :
alertify.prompt("Message", function (e, str) {
// str is the input text
if (e) {
Console.Log("Ok");
} else {
Console.Log("No");
}
}, "Default Value");
When I click on Ok or No, nothing is firing. And the TextBox content of the prompt is :
function (e, str) { // str is the input text if (e) { Console.Log("Ok"); } else { Console.Log("No"); } }
And with an 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 %>');
}
});
Only "Ok" is firing. The cancel button does nothig.
SOLUTION : Took another alertify.js (http://alertifyjs.com/) And this is my JS function :
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' });
And it works !
Solution : Create 2 HTML buttons, one visible linked to the JavaScript function and another not visible linked to the C# method :
<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;" />
The 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' });
}
When you will click on the 1st button, it will call JS. Then JS will call the PostBack of the second button. And I had a problem with Alertify, so I used another source : http://alertifyjs.com/