1

I know there are a lot of questions like this one, but I can't find the right answer.

I have a button on my asp.net page:

<asp:LinkButton ID="lbReset" runat="server" CssClass="lbReset" OnClientClick="return ShowDialog();" ToolTip="Restart your session as if you would just have logged in">Restart</asp:LinkButton>

I have this div for the dialog content:

<div id="dialog" title="Restart" style="display: none;">
        <p>This will clear all data of the current session (as if you would have just logged in)!</p>
        <p>Are you sure?</p>
    </div>

I have this script for the javascript part:

<script type="text/javascript">

        $("div#dialog").dialog({
            modal: true,
            closeOnEscape: false,
            autoOpen: false,
            buttons:
                {
                    "Yes": function () { $("div#dialog").dialog("close"); callback(false); }
                    , "No": function () { $("div#dialog").dialog("close"); callback(true); }
                }
        }).prev().find(".ui-dialog-titlebar-close").hide();

        function ShowDialog() {
            return $("div#dialog").dialog("open");
        }

        function callback(value) {
            return value
        }

    </script>

If I use a simple confirm box, I can stop the button from executing the code-behind when the user clicks no. I want this dialog to have the same behaviour. But it doesn't matter on which button you click, yes or no, the code-behind is executed anyway.

What am I doing wrong here?

rg. Eric

Eric
  • 695
  • 9
  • 25

1 Answers1

1

You can try as following:

<script type="text/javascript">

    $(function () {
        $("#dialog").dialog({
            modal: true,
            closeOnEscape: false,
            autoOpen: false,
            buttons: 
                {
                    Yes: function () {
                       $(this).dialog("close");
                       $(this).data("callback")(true);
                },
                    No: function () {
                       $(this).dialog("close");
                       $(this).data("callback")(false);
                }
            }
       }).prev().find(".ui-dialog-titlebar-close").hide()
    });

    function ShowDialog(message, callback) {
        $('#dialog').text(message);
        $('#dialog').data("callback", callback).dialog("open");
    };

</script>
Earth
  • 3,477
  • 6
  • 37
  • 78
  • What will the function ShowDialog return in this example? – Eric Aug 26 '14 at 10:44
  • 1
    Add `return` before `$('#dialog').data("callback", callback).dialog("open");` for `ShowDialog` to `return` which is equal to `return $("div#dialog").dialog("open");` – Earth Aug 26 '14 at 10:53
  • Am I correct as to calling the dialog with: OnClientClick="var callback; ShowDialog(callback); return callback;" in the button definition? – Eric Aug 26 '14 at 11:51
  • it doesn't work. no matter which button I click, both will execute the code-behind. – Eric Aug 26 '14 at 12:35