Atlas: Creating a Confirmation Using the ModalPopup Extender

I've been looking into Atlas Framework recently, specifically around the idea of how to apply it to future projects. Today I want to show how to use the Modal Popup Extender found in the Atlas Control Toolkit to create a modal confirmation box.

Although, there is a ConfirmButton Extender that uses the built-in javascript confirm function, I find this type of confirmation really limiting. There is no way to customize the look and feel of the popup. The popup window size is constrained which limits the text you can write in your message. You can only have "Okay" and "Cancel" text for the buttons, when in most cases the action requested is of a "Yes\NO' type.

For my confirmation, I want the following features:

  • Has to be modal
  • Fully customizable with css
  • Has to have "Yes" and "No" buttons
  • Postback if the user clicks "Yes"
  • No postback if the user clicks "No"
  • Provide feedback to the user of the action taken.

ASPX Code
The code on the ModalConfirm.aspx page is fairly straightforward. I have a Panel control which will be my popup box. It contains the popup text, the Yes button, and the No button. I'm using the ModalPopupProperties on the ModalPopupExtender to configure the confirmation box. The Delete button will trigger the confirmation and the Yes/No buttons will dismiss it. When the user clicks "Yes", a javascript function called onYes will be called, and likewise when the user clicks "No".

Notice the settings for the various class attributes. This provides the flexibility to use css to style the confirmation box. Also notice the Style="display: none" attribute to hide the confirmation until the user clicks on the delete button.

<asp:Panel ID="ConfirmtionPanel" runat="server" CssClass="modalPopup" Style="display: none">
    <div class="modalPopup-text">
        Are you sure you want to delete this item?<br />
        <br />
        <asp:Button ID="YesButton" runat="server" Text="Yes" OnClick="YesButton_Click" />&nbsp;&nbsp;
        <asp:Button ID="NoButton" runat="server" Text="No" />
    </div>
</
asp:Panel>
<
atlastoolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server">
    <atlastoolkit:ModalPopupProperties TargetControlID="DeleteButton" PopupControlID="ConfirmtionPanel"
        OkControlID="YesButton" OnOkScript="onYes()" CancelControlID="NoButton" OnCancelScript="onNo()"
        BackgroundCssClass="modalBackground" />
</
atlastoolkit:ModalPopupExtender>

ASPX Code-Behind
In the code-behind, I need to expose the asp.net client-side __doPostBack function in the Page_Load event so that I can programmatically call it from javascript later using Atlas client scripting. I also have the event handler for my YesButton_Click event which will perform the deletion by calling a middle tier object to delete the item. As a last step, I inform the user that the deletion has been performed.

    protected void Page_Load(object sender, EventArgs e)
    {
        
// Expose the __doPostBack function to programmatically call it from javascript
        Page.ClientScript.GetPostBackEventReference(this, String.Empty);
    }

    
protected void YesButton_Click(object sender, EventArgs e)
    {
        
// Code to delete the item goes here...

        Label1.Text = "Item deleted";
    }

Javascript
I have a script block in my header. The block has the two functions that has been mentioned earlier. The onYes function creates a PostBackAction object with the target control set to the YesButton. The performAction method will call __doPostBack. The onNo function simply sets the label text.

<script type="text/javascript">
    function onYes() {
        
var postBack = new Sys.WebForms.PostBackAction();
            postBack.set_target(
'YesButton');
            postBack.set_eventArgument(
'');
            
            postBack.performAction();
    }
    
    
function onNo() {
        
//no postback necessary
        $('Label1').innerText = 'Action canceled';
    }
</script>

Modal Confirmation Styles
Here is the corresponding styles for my confirmation box.

<style type="text/css">
body {
font:normal 10pt/13pt Arial, Verdana, Helvetica, sans-serif;
color:#666;
margin:20px;
}
    
/*Modal Popup*/
.modalBackground {
background-color:#000;
filter:alpha(opacity=80);
opacity:0.8;
}
.modalPopup img {
border:solid 5px #fff;
}
.modalPopup-text {
display:block;
color:#000;
background-color:#fff;
text-align:center;
border:solid 2px #000;
padding:10px;
}
.modalPopup-text input {
width:75px;
}    
.feedback
{
color: #00cc00;
font-weight: 700;
}
</style>

End Result

You can try it here:
http://atlas.vertigosoftware.com/atlasmodalconfirm/modalconfirm.aspx

Source Code

I have created a zip file with the source code to this sample. In order to run this sample, you will need:

Source: http://blogs.vertigosoftware.com/files/alan/atlasmodalconfirm.zip
posted on Tuesday, July 25, 2006 1:29 PM by AlanL

Comments

# Atlas “How Do I?” Video Series Begins

Thursday, July 27, 2006 10:48 PM by ScottGu's Blog
Earlier this spring we published a really popular series of short, focused, online ASP.NET videos that

# Atlas “How Do I?” Video Series Begins

Thursday, July 27, 2006 11:04 PM by ATLAS Team Blogs
Earlier this spring we published a really popular series of short, focused, online ASP.NET videos that

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Saturday, July 29, 2006 1:01 PM by Scottt40
Thank you very much for this tutorial. I want to implement modalPopups with some imput forms I have, so this is very timely, and will make for a very pleasing user experience. Will modals word from within a WebPart?

# Atlas: Using the DragOverlayExtender with the ASP.NET Profile Service

Monday, July 31, 2006 8:15 PM by Alan Le
My last two posts on Atlas has been well received. I plan to continue writing quick demos as I explore...

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Monday, July 31, 2006 8:25 PM by Alan Le
Hi Scott40, I haven't looked into modals within a WebPart, so I don't know off hand.

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Friday, August 04, 2006 8:15 AM by Brian
Cool post, one thing I might add is that if you are trying to do this in a master page you need the full reference to the control id in the onOK() javascript such as postBack.set_target('ct100_masterName_ YesButton');

took me forever to realize that :)


# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Thursday, August 31, 2006 1:12 PM by Steve
Hi Alan..
nice post, i am now trying to do something similar, but i want only an update panel from the main form to refresh.
I am also trying to have an update panel inside the modal panel that will post back independantly keepin the modal in place....any ideas?

check my post.
http://forums.asp.net/thread/1386346.aspx

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Tuesday, September 12, 2006 10:00 AM by MacSpudster
The code provided works, though I have need of using the javascript on many pages, and with many controls on many pages.

So, I modified the JS code as follows, wherein the targetID and EventArgument (if any) are passed in.

function onControlClick(sTargetID, sEventArgument) {
var postBack = new Sys.WebForms.PostBackAction();
postBack.set_target(sTargetID);
postBack.set_eventArgument(sEventArgument);

postBack.performAction();
}

# Global JScript & MasterPage usage

Tuesday, September 12, 2006 10:07 AM by MacSpudster
2 more items:
1) The above JS code is in the .js file for the website
2) A posting above noted that when the JScript is used on a MasterPage, to reference the full ID.
- I've tried and tried this on a master page and I'm not getting the event to fire. Only fires on a direct page.

Any insights appreciated.

# Atlas: Using the DragOverlayExtender with the ASP.NET Profile Service

Monday, September 25, 2006 9:49 AM by Alan Le
My last two posts on Atlas has been well received. I plan to continue writing quick demos as I explore...

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Thursday, September 28, 2006 11:37 AM by Koen
Any idea if it is possible to override the javascript confirm function by this one and/or pass on a specific message?

Thanks...

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Saturday, September 30, 2006 1:31 PM by Rushabh

# Beta 1 - 'Sys' is undefined

Thursday, October 26, 2006 10:31 AM by ASP.NET AJAX Forum Posts
I am trying to implement error handeling similar to the example shown here http://ajax.asp.net/docs/mref/a92421a3-3be5-9c1c-4a6c-8f47be4f7910.aspx.Except

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Friday, March 02, 2007 4:15 AM by Alec
Hi Alan,
I'm using the RTM version of MS-AJAX Extensions, plus the AJAX Toolkit.
I've tried the above technique you've devised, including the id tweaks required for use with MasterPages, but still get a javascript page error.
This seems to be on the first line, for creating the new var of type Sys.WebForms.PostBackAction.
I googled on this and found one page that mentions a change in ctp's that used a supplemental js file to sort it out. Is there something similar required for the RTM version?
I'd really like to use this approach!
Thanks.

# re: Atlas: Creating a Confirmation Using the ModalPopup Extender

Tuesday, May 01, 2007 5:53 AM by Ali
Hi Brian and Alan,
Great Article!
I am struggling to set the postback code for MasterPage. The actual button is at the default page and i am setting following code: postBack.set_target('ctl00_ContentPlaceHolder1_YesButton');
I have tried several different ways but no use. Any help will be appreciated.

Cheers,

Ali

# ModalPopupExtender control and Response.Reidrect

Wednesday, November 07, 2007 10:15 AM by ASP.NET AJAX Toolkit Forum Posts
I&amp;#39;ve searched this board for something close to what I&amp;#39;m experiencing, so I apologize in advance

# ModalPopupExtender control and Response.Reidrect

Thursday, February 07, 2008 1:11 PM by ASP.NET AJAX Toolkit Forum Posts
I&amp;#39;ve searched this board for something close to what I&amp;#39;m experiencing, so I apologize in advance