← Tutti gli articoli
jQuery UI Dialog shown using an ASP.NET button.
26 April 2011 ·
jQuery · Article ·
901 visite
I'll show a modal dialog (with Yes, No, Cancel buttons). When the user clicks on the ASP.Net button appears a modal popup but inmediately the page do postback.
For instance a I have the following Asp.net controls (a button and an hidden field):
- < asp:HiddenField ID = "hf_YesNo" runat = "server" Value = "0" />
- < asp:Button ID = "btnSend" runat = "server" Text = "Test" OnClientClick = "return CheckAll(this);" OnClick = "btnSend_Click" />
To prevent the postback problem I have used the following solution:
function CheckAll(btn) {
var $dialog = $("
Are you married?
").dialog({
autoOpen: true,
modal: true,
buttons: [
{
text: "Yes",
click: function() {
$("#<%= hf_YesNo.ClientID %>").val("1");
__doPostBack(btn.name, '');
$dialog.remove();
}
},
{
text: "No",
click: function() {
$("#<%= hf_YesNo.ClientID %>").val("0");
__doPostBack(btn.name, '');
$dialog.remove();
}
},
{
text: "Cancel",
click: function() {
bcancel = false;
$dialog.remove();
}
}
]
});
return false;
}
The CheckAll(btn) javascript function return false on the OnClientClick preventing the postback on the server. To force the postback on the Yes, No click button I use __doPostBack(btn.name, '');