Reputation: 99
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 !
Upvotes: 1
Views: 3508
Reputation: 99
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/
Upvotes: 2
Reputation: 4902
You can trigger click on hidden buttons where you can have attached click handlers on code behind.
Server:
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();
}
Client:
<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 Source and css downloaded from: http://fabien-d.github.io/alertify.js/
Important Note: If you are trying to trigger click on file upload input IE for sore will not let u do that.
You can use pure java script no jquery:
document.getElementById("PrchBtnHiddenYes").click();
Upvotes: 0