I have attached a popup to an event handler of a button.
This is my client side code in javascript (asp.net):
<script type="text/javascript" language="javascript">
String.Format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
};
var dialogConfirmed = false;
function SetDialogConfirmedFalse() {
dialogConfirmed = false;
}
function ConfirmDialogInfo(widthX, obj, title, dialogText) {
if (!dialogConfirmed) {
$('body').append(String.Format("<div id='dialog' title='{0}'>{1}</div>",
title, dialogText));
$('#dialog').dialog({
height: 110,
width: widthX,
modal: true,
resizable: false,
draggable: false,
close: function (event, ui) { $('body').find('#dialog').remove(); },
buttons:
{
'Ok': function () {
$('#dialog').dialog('close');
dialogConfirmed = true;
if (obj) obj.click();
}
}
});
$(".ui-widget").css({ "font-size": +18 + "px" });
}
return dialogConfirmed;
}
function ConfirmDialog(obj, title, dialogText) {
if (!dialogConfirmed) {
$('body').append(String.Format("<div id='dialog' title='{0}'><p>{1}</p></div>",
title, dialogText));
$('#dialog').dialog({
height: 110,
modal: true,
resizable: false,
draggable: false,
close: function (event, ui) { $('body').find('#dialog').remove(); },
buttons:
{
'Ja': function () {
$('#dialog').dialog('close');
dialogConfirmed = true;
document.getElementById('<%= hdnTestValue.ClientID %>').value = dialogConfirmed;
},
'Nein': function () {
$('#dialog').dialog('close');
document.getElementById('<%= hdnTestValue.ClientID %>').value = dialogConfirmed;
}
}
});
$(".ui-widget").css({ "font-size": +18 + "px" });
}
}
</script>
<asp:HiddenField runat="server" ID="hdnTestValue" />
This is my server side code in C#:
protected void buttonStopCurrentContract_ButtonPressed(object sender, EventArgs e)
{
var standardMessage = "Do you really want to cancel this contract?";
var script = "ConfirmDialog('this', '" + CommonRes.Attention +
"', '"+ standardMeldung + "');";
ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);
if (hdnTestValue.Value == "true")
{
}
}
When I run this code, ScriptManager.RegisterStartupScript waits till the end of the method and pops the dialog. After picked up a decision "yes" the decision is being saved in the hiddenfield. I have to click twice on the button to get into the if-condition. There is a postback problem. I have not much experience in javascript or asp. Can anybody give me advices / solutions?
Thank you in anticipation.