Click here to Skip to main content
14,669,305 members
Rate this:
Please Sign up or sign in to vote.
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) { //!$('#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#:

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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: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
protected void buttonStopCurrentContract_ButtonPressed(object sender, EventArgs e)
{                      
    // hdnTestValue is a hiddenfield
    if (hdnTestValue.Value == "true")
    {
        // do something
    }                          
}
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100