Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have attached a popup to an event handler of a button.

This is my client side code in javascript (asp.net):

JavaScript
<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) { //!$('#dialog').is(':data(dialog)')
                    $('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) { //!$('#dialog').is(':data(dialog)')
                    $('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;
                                //document.getElementById("hdnTestValue").value = dialogConfirmed;
                                //if (obj) obj.click();
                            },
                            'Nein': function () {
                                $('#dialog').dialog('close');
                                document.getElementById('<%= hdnTestValue.ClientID %>').value = dialogConfirmed;
                                //document.getElementById("hdnTestValue").value = dialogConfirmed;    
                            }
                        }
                    });
                    $(".ui-widget").css({ "font-size": +18 + "px" });
                }
            }
        </script>

    <asp:HiddenField  runat="server" ID="hdnTestValue" /> 


This is my server side code in C#:

JavaScript
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);
                        
                        // hdnTestValue is a hiddenfield
                        if (hdnTestValue.Value == "true")
                        {
                          // do something
                        }
                    
                
            }


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.
Posted

1 solution

Your problem appears to be caused because you call a postback to show the confirm dialog. Try calling the ConfirmDialog() routine from client-side using the buttonStopCurrentContract's OnClientClick event.

Call javascript routine from button's OnClientClick event...
ASP.NET
<asp:button id="buttonStopCurrentContract" runat="server"
        OnClientClick="javascript:ConfirmDialog(this,'Confirm Action','Do you really want to cancel this contract?');" />

... and in your code behind's buttonStopCurrentContract_ButtonPressed event
C#
protected void buttonStopCurrentContract_ButtonPressed(object sender, EventArgs e)
{                      
    // hdnTestValue is a hiddenfield
    if (hdnTestValue.Value == "true")
    {
        // do something
    }                          
}
 
Share this answer
 
v2
Comments
0xMoonstar 4-Sep-13 4:24am    
It still doesn't work and I still have to click the button twice to get into the if-condition. Thank you anyways. An addition: The button is within an updatePanel... Could be this the problem??? I added "UseSubmitBehavior="false" and now it's kinda working. The next problem is that I don't know how to use javascript variables in "onClientClick" because I need individual messages being shown in the popup window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900