dialog as javascript confirm alert

This post has 20 replies

Top 500 Contributor
Posts: 28
lizs Posted: Tue Dec 4, 2007 @ 11:18 AM
Does anyone have a sample of using the Dialog object to mimic a javascript confirm alert? I've got it set up with Ok and Cancel buttons. Where I'm stuck is in having the Cancel actually return false to the calling link. The javascript function which shows the Dialog can't call get_result until AFTER the Ok or Cancel have been clicked.

Here's my code: (master page)

<ComponentArt:Dialog id="caConfirm" Modal="true" runat="server" HeaderClientTemplateId="HeaderClientTemplate1" ContentClientTemplateId="ContentClientTemplate1"
AnimationType="Live" ShowTransition="Fade" AnimationSlide="ExponentialDecelerate"
CloseTransition="Fade" ModalMaskImage="images/alpha.png" AnimationDuration="600" >
<ClientTemplates>
<ComponentArt:ClientTemplate id="HeaderClientTemplate1">
## Parent.Title ##
< /ComponentArt:ClientTemplate>
< ComponentArt:ClientTemplate id="ContentClientTemplate1">
## Parent.Content ##
<center><br />&lt; input type='button' onclick='## Parent.Id ##.Close(true);' value='OK' />&nbsp;&nbsp;&lt; input type='button' onclick='## Parent.Id ##.Close(false);' value='Cancel' />< /center>
< /ComponentArt:ClientTemplate>
< /ClientTemplates>
< /ComponentArt:Dialog>

Code-behind:

caConfirm.ClientEvents.OnClose = new ComponentArt.Web.UI.ClientEvent("confirmReturn");
this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ConfirmReturn", "function confirmReturn() {" + String.Format("return {0}.get_result();", caConfirm.ClientID) + " }", true);
// this is client side, so have to provide the javascript function
this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ConfirmFlare", "function confirmFlare(confirmText,confirmHeader) {" + String.Format("{0}.Show(confirmText,confirmHeader);", caConfirm.ClientID) + " }", true);



Thanks for your help!
Top 10 Contributor
Posts: 6,149
stephen Posted: Wed Dec 5, 2007 @ 2:35 PM
Sorry, I think you've lost me - you want ok and cancel buttons, which is reasonable. The function that shows the dialog you also want to handle ok/ cancel? I would think that in most cases, clicking cancel would just close the dialog, while clicking okay would do something else entirely. Can you do it like this?
Stephen Hatcher, Developer Support Manager
Top 500 Contributor
Posts: 28
lizs Posted: Wed Dec 5, 2007 @ 2:49 PM
I want the ok and cancel buttons in the dialog to return true or false to the function that opened the dialog.

I can make the cancel button close the dialog, but the calling function needs to wait for the user to either click ok or cancel before proceeding.
Top 10 Contributor
Posts: 6,149
stephen Posted: Thu Dec 6, 2007 @ 1:08 PM
I think this is the bit where I'm confused, so please bare with me:

"I want the ok and cancel buttons in the dialog to return true or false to the function that opened the dialog."

So you call a function that shows the dialog control:

function showDialog()
{
dialog.show();
}

Within that dialog you have two buttons, ok and cancel. You want these buttons to return a boolean value to showDialog()?

I guess my confusion is from my understanding, you have different actions, and therefore functions here:

1. show a dialog
2. a button inside of dialog is clicked that has one action
2. a different button inside of the dialog is clicked that has a different action

If I'm interpreting all this correctly, it sounds like you want this to be written identically to the way you'd write a script using confirm method, but it can't be; confirm() returns a value based on user interaction, show() doesn't. The exact same effect can be very easily achieved as I've described it.

Does this make sense?
Stephen Hatcher, Developer Support Manager
Top 75 Contributor
Posts: 62
Orgintell Posted: Sat Dec 8, 2007 @ 8:59 AM
Lizs,

I don't think the scenario you describe is possible. The reason is simple. In order to implement the confirm() function, the browser suspends JavaScript execution while it waits for user input. Once the user chooses OK or Cancel, it continues execution in the JavaScript where it left off. This allows your code to immediately continue by checking the result of the confirm() operation. This is a synchronous operation.

<asp:Button ID="Button1" runat="server" Text="Delete All My Data" OnClick="Server_Click_Handler" OnClientClick="return confirm('Are you sure?');" />


The show() function shows the dialog and immediately returns. Your OnClientClick handler continues, most likely posting back in the process. Meanwhile, the user hasn't had a chance to click any buttons in your dialog yet because the JavaScript had to keep running. You can't pause in the middle of an event handler in JavaScript.

I hope this helps illuminate the situation for you. What Stephen was saying is you need to handle the OK button's Click event to proceed with what you were going to do; otherwise, do nothing. This is indeed the best approach.
Jeremy Fuller / Vice President, Software Development / Organizational Intelligence, Inc.
Top 500 Contributor
Posts: 28
lizs Posted: Tue Dec 11, 2007 @ 11:36 AM
Thanks for your replies. I understand that the show method is asynchronous. What I needed was a solution for showing the dialog and then either proceeding with the postback or cancelling.

I figured this out in the end, and it actually wasn't that difficult. I pass the calling link object into the function that shows the dialog, and then on close of the dialog, another function re-executes the link (detaching the onclick first) if get_result was true. This allows me to handle everything client-side, and keep my dialog box and functions generic, without custom wiring my Yes/No buttons.
Top 75 Contributor
Posts: 62
Orgintell Posted: Tue Dec 11, 2007 @ 11:53 AM
Lizs,

That's very clever! I might actually use that technique at some point. Nice job, and sorry I wasn't more helpful.
Jeremy Fuller / Vice President, Software Development / Organizational Intelligence, Inc.
Top 10 Contributor
Posts: 6,149
stephen Posted: Tue Dec 11, 2007 @ 12:45 PM
Ah, very clever indeed. Thanks for posting, and I too apologize for not being more useful.
Stephen Hatcher, Developer Support Manager
Not Ranked
Posts: 3
phlorryn Posted: Thu Jan 31, 2008 @ 7:36 AM
Hello

I tried to do something like you described - but i have problems with using this when using with other controls than buttons/linkbuttons and when handling other events than click.

Could you be so kind to give an example of your solution, perhaps i'm missing something.

Thank you
Top 200 Contributor
Posts: 37
seggerman Posted: Fri Aug 1, 2008 @ 7:09 AM
could you provide the code?

"wasn't that difficult?" can't seem to figure out how you did it

thanks
Top 10 Contributor
Posts: 6,149
stephen Posted: Fri Aug 1, 2008 @ 1:15 PM
This isn't the code mentioned above, and probably isn't as elegant, but here's a way to do this:


    <script type="text/javascript" language="javascript">      
        var needDialog = true;
        var result = false;
       
        function confirmDialog()
        {
            if(needDialog) 
            {
                showDialog();
                return false;
            }
            else
            {
                needDialog = true;
                return result;
            }
        }
        
        function showDialog()
        {
            needDialog = false;
            Dialog1.show();
        }
        
        function Dialog1_Close(sender, args)
        {
            result = sender.get_result();
            document.getElementById("Button1").click();
        }
    </script>

<ComponentArt:Dialog runat="server" Modal="true" ID="Dialog1">
        <Header>
            Really do this? 
        </Header>
        <Content>
            <input type="button" value="ok" onclick="Dialog1.close(true);" />
            <input type="button" value="cancel" onclick="Dialog1.close(false);" />
        </Content>
        <ClientEvents>
            <OnClose EventHandler="Dialog1_Close" />
        </ClientEvents>
    </ComponentArt:Dialog>
    <div>
        <asp:Button ID="Button1" OnClientClick="return confirmDialog();" runat="server" Text="Button" />

Stephen Hatcher, Developer Support Manager
Top 200 Contributor
Posts: 37
seggerman Posted: Mon Aug 4, 2008 @ 7:53 AM
thanks!

a lot simpler than the way I ended up implementing
I was just about to post my solution but it involved turning off the click event of the button and putting something into __EVENTARGUMENTS
Top 200 Contributor
Posts: 37
seggerman Posted: Mon Aug 4, 2008 @ 8:44 AM
I implemented the solution and had to make one change

it's Dialog1.Show, not Dialog1.show - right?
Top 200 Contributor
Posts: 37
seggerman Posted: Tue Aug 5, 2008 @ 6:58 AM
another correction - wrong signature on the close event
- the (sender, args) is for a server click event
- the Dialog object is the signature for the client close event

function Dialog1_Close(dialog)
{
result = dialog.get_result();
document.getElementById("Button1").click();
}
Top 10 Contributor
Posts: 6,149
stephen Posted: Tue Aug 5, 2008 @ 7:02 AM
What version of dialog are you using? show() is valid (all of our methods begin with a lower case character as of 2006.2). As well, our client events all take two arguments (though not required). sender is exactly what you have above- a variable name; in my case, it's sender, but yours is dialog. Both refer to the dialog object.
Stephen Hatcher, Developer Support Manager
Page 1 of 2 (21 items) 1 2 Next >