Display ‘Please Wait’ popup

This procedure explains how to add a ‘Please wait’ screen to an ASP.net application.

By default at server process (e.g. saving a request) you only see a progress bar in the status bar. statusBar. If it’s a long running process you might not see any update for a longer time, and users might think that the application stopped, is already finished or did not even start because they did not press the correct button. This might result in double clicks on the Save button or closure of the browser window without completion.

Therefore it is better to display message in the middle of the screen so the user is aware that there is something going on. Current procedure describes how to disable all existing web controls (buttons etc.), coloring all areas in dark gray (fade out) and display an animated circle next to a “Please wait” text.

clip_image002

The procedure has been created based on ASP.net 2005 (2.0) and Microsoft’s Ajax Control Toolkit Release 11119.

CSS

Add the following lines to your CSS file:

/*Modal Popup*/
.modalBackground {
    background-color:Gray;
    filter:alpha(opacity=70);
    opacity:0.7;
}

.modalPopup {
    background-color:#ffffdd;
    border-width:3px;
    border-style:solid;
    border-color:Gray;
    padding:3px;
    text-align:center;
}
.hidden {display:none}

JavaScript

Add the following JavaScript Code to your aspx page:

<script type="text/javascript" language="javascript">

         function pageLoad(sender, args) {
            var sm = Sys.WebForms.PageRequestManager.getInstance();
            if (!sm.get_isInAsyncPostBack()) {
                sm.add_beginRequest(onBeginRequest);
                sm.add_endRequest(onRequestDone);
            }
        }

        function onBeginRequest(sender, args) {
            var send = args.get_postBackElement().value;
            if (displayWait(send) == "yes") {
                $find('PleaseWaitPopup').show();
            }
        }

        function onRequestDone() {
             $find('PleaseWaitPopup').hide();
        }

        function displayWait(send) {
            switch (send) {
                case "Save":
                    return ("yes");
                    break;
                case "Save and Close":
                    return ("yes");
                    break;
                default:
                    return ("no");
                    break;
            }
        }
    </script>

Taking a closer look at the script you see the last function “displayWait”. There you could define on which buttons the ‘Please wait’ screen should be displayed. In current example the button has to have the text “Save” or “Save and Close”. On all other buttons the “Please wait” screen will not be displayed. Of course you should modify this function according to your needs.

Buttons in Ajax UpdatePanel

Add an Update Panel to your aspx page. Place your buttons in the Update-Panel. At least the buttons which should display the ‘Please wait’ screen need to be in the UpdatePanel, but you could of course also add other buttons etc. there.

<asp:UpdatePanel ID="PleaseWaitPanel" runat="server" RenderMode="Inline">
    <ContentTemplate>
       <asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Click" />
       <asp:Button ID="SaveClose" runat="server" Text="Save and Close" OnClick="SaveClose_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

Aspx Controls

Add a Panel, Button and a ModalPopupExtender to your aspx page. The Panel will be displayed in the middle of the screen. The hidden button is necessary for the ModalPopupExtender as it needs a TargetControl.

<asp:Panel ID="PleaseWaitMessagePanel" runat="server" CssClass="modalPopup" Height="50px"
 Width="125px">
 Please wait<br />
 <img src="img/ajax-loader.gif" alt="Please wait" /> </asp:Panel>
 <asp:Button ID="HiddenButton" runat="server" CssClass="hidden" Text="Hidden Button"
 ToolTip="Necessary for Modal Popup Extender" />
 <ajaxToolkit:ModalPopupExtender ID="PleaseWaitPopup" BehaviorID="PleaseWaitPopup"
 runat="server" TargetControlID="HiddenButton" PopupControlID="PleaseWaitMessagePanel"
 BackgroundCssClass="modalBackground">
 </ajaxToolkit:ModalPopupExtender>

Image

Add an animated gif to your project. Check Ajaxload.info to create your own if you like, otherwise you might use this one: ajax-loader

Build

That’s it. Now build your application. Or maybe you want to download the example.

14 comments

  1. Alan

    What would be nice, and what i’m having a problem to create, is to fade out the modal popup using a FadeOut animation. Any clues please? :-)

  2. Leona Drake

    Fairly superb article, definitely helpful information. Never believed I’d discover the tips I need right here. I have been looking all over the internet for a while now and had been starting to get disappointed. Fortunately, I stumbled onto your website and received exactly what I was hunting for.

  3. Mark

    Fantastic – I have been searching all over for precisely this code. Excellent job. Thanks so much.

  4. Scott

    Absolutely Fantastic!!! Exactly what I was looking for – thank you very much! I do have one question though; I am not very good with java and rather than just having the text of a button determine whether or not the modal dialog is displayed, is there a way to identify when it’s displayed by element id? I want to display the wait message when a user selects a row from a grid.

  5. john

    This is awsome code, I have intergrated it into my website and it looks great. How do I start the javascript function “displayWait(send)” from VB Code behind in VS 2008?

  6. john

    Your right it is, When I plug your code it works well, But my code uses the log in wizard to validate customer info. I try to call System.Threading.Thread.Sleep(5000) from my CreateUserWizard1_CreatedUser1 procedure and it completely ignores the code. I have aso tried to call the procedure “Save_Click” from the CreateUserWizard1_CreatedUser1 procedure but the “Please Wait” dose not display. How Do I turn on the function “displayWait(send)” when my validation begins and how do I turn it off when it completes from code behind?

  7. bdk

    When I plug your code it works except my progress .gif displays on every page load instead of on just the button click. Is there anyway of surpressing it on page load?

  8. micki

    Excellent code, really useful, congratulations!
    But it won’t work in my scenario:
    I need to upload a file with my form, so I had to include a FileUpload control inside the form. In order to make the FileUpload work into the UpdatePanel, I have to do a Full PostBack.
    But then, everything works as usual, but the Modal Popup won’t show up :(
    I’m quite a newbie in ASP.NET, so any help about this would be greatly appreciated. Thanks!

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>