Amit's Blog

Sharing Thoughts and Learning

  Home  |   Contact  |   Syndication    |   Login
  43 Posts | 1 Stories | 153 Comments | 14 Trackbacks

News

About Me?
Read it in
Blog Statistics
Proud Member of

Archives

Post Categories

Articles

Book Review

I Visit.

OpenSource Project(s)

Modal Progress Dialog

If you ever like to show a Modal Progress dialog like the above for any ajax operation no matter which Update Panel or Web Service call is responsible for it, the following code will do the same.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    [System.Web.Services.WebMethod()]
    public static string GetDate()
    {
        //Doing a fake delay for 3 seconds
        System.Threading.Thread.Sleep(3000);

        return DateTime.Now.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblDate1.Text = lblDate2.Text = DateTime.Now.ToString();
        }
    }

    protected void btnDate1_Click(object sender, EventArgs e)
    {
        //Doing a fake delay for 3 seconds
        System.Threading.Thread.Sleep(3000);
        lblDate1.Text = DateTime.Now.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Modal Dialog Progress</title>
    <style type="text/css">
        .modalBackground
        {
            background-color:#e6e6e6;
            filter:alpha(opacity=60);
            opacity:0.60;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <fieldset>
            <legend>UpdatePanel</legend>
            <div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="lblDate1" runat="server"></asp:Label>
                        <asp:Button ID="btnDate1" runat="server" Text="Get Date" OnClick="btnDate1_Click" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </fieldset>
        <fieldset>
            <legend>PageMethods</legend>
            <div>
                <asp:Label ID="lblDate2" runat="server"></asp:Label>
                <asp:Button ID="btnDate2" runat="server" Text="Get Date" OnClientClick="return btnDate2_Click()" />
            </div>
        </fieldset>
        <asp:Panel ID="pnlProgress" runat="server" style="background-color:#ffffff;display:none;width:400px">
            <div style="padding:8px">
                <table border="0" cellpadding="2" cellspacing="0" style="width:100%">
                    <tbody>
                        <tr>
                            <td style="width:50%"></td>
                            <td style="text-align:right">
                                <img alt="" src="indicator-big.gif" />
                            </td>
                            <td style="text-align:left;white-space:nowrap">
                                <span style="font-size:larger">Loading, Please wait ...</span>
                            </td>
                            <td style="width:50%"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </asp:Panel>
        <ajaxToolKit:ModalPopupExtender ID="mpeProgress" runat="server" TargetControlID="pnlProgress" PopupControlID="pnlProgress" BackgroundCssClass="modalBackground" DropShadow="true" />
    </form>
    <script type="text/javascript">

        Sys.Net.WebRequestManager.add_invokingRequest(onInvoke);
        Sys.Net.WebRequestManager.add_completedRequest(onComplete);

        function onInvoke(sender, args)
        {
            $find('<%= mpeProgress.ClientID %>').show();
        }

        function onComplete(sender, args)
        {
            $find('<%= mpeProgress.ClientID %>').hide();
        }

        function btnDate2_Click()
        {
            PageMethods.GetDate (
                                    function(result)
                                    {
                                        var lbl = $get('<%= lblDate2.ClientID %>');

                                        if (document.all)
                                        {
                                            lbl.innerText = result;
                                        }
                                        else
                                        {
                                            lbl.textContent = result;
                                        }
                                    }
                                );

            return false;
        }

        function pageUnload()
        {
            Sys.Net.WebRequestManager.remove_invokingRequest(onInvoke);
            Sys.Net.WebRequestManager.remove_completedRequest(onComplete);
        }

    </script>
</body>
</html>

The main trick is hooking the two special events of Sys.Net.WebRequestManager class. This class is responsible for managing all kinds of communication with the server, even a manual invoke of a Sys.Net.WebRequest class will bring this class into action. It fires the invokingRequest before making an Ajax call and fires the completeRequest once the method completes. We are simply showing/hiding the modal popup extender in these events.

Download: Full Source

kick it on DotNetKicks.com

posted on Friday, August 24, 2007 7:13 PM

Feedback

# re: Showing Modal Progress Dialog in all Ajax Operation 8/30/2007 4:52 AM Forrest
This is very cool! Outside the traditional "ATLAS" or just do it yourself (from scratch) there's not much AJAX stuff around the internet targeting the .net framework. There's everything you could dream of for PHP, but it's nice to see C# get some love, too.

# re: Showing Modal Progress Dialog in all Ajax Operation 9/3/2007 4:08 PM Carlos
you forgot the demo!

# re: Showing Modal Progress Dialog in all Ajax Operation 9/3/2007 4:28 PM Kazi Manzur Rashid
Hi, The GeekswithBlogs does not allow to upload file, thus i have to host it in an external site. Just click the full source link at the bottom of the post my shared folder at box.net will open where you can downloas the sourcecode of this post.

# re: Showing Modal Progress Dialog in all Ajax Operation 9/3/2007 11:44 PM Morteza
very nice job, thank you

# re: Showing Modal Progress Dialog in all Ajax Operation 9/28/2007 3:00 AM MIM
VERY NICE..

# re: Showing Modal Progress Dialog in all Ajax Operation 10/4/2007 1:57 PM Gokhan ZER
grate job! thank you!

# re: Showing Modal Progress Dialog in all Ajax Operation 10/12/2007 4:02 AM Resim Yükle
Thanks. Good article

# re: Showing Modal Progress Dialog in all Ajax Operation 1/11/2008 5:50 AM Ricardo Chois
Excelente. Gracias por compartirlo.

# re: Showing Modal Progress Dialog in all Ajax Operation 2/19/2008 8:28 PM kız oyunları
Thanks. Good article

# re: Showing Modal Progress Dialog in all Ajax Operation 2/28/2008 3:23 AM msn nick
thanks admin

# re: Showing Modal Progress Dialog in all Ajax Operation 3/8/2008 3:50 AM vladimir
if you have two different buttons on the page and want to display different progress panels (text/images) for each, how would u do that. In this example it looks like all post back events would use the same progress panel.

Thanks,
-ac

# re: Showing Modal Progress Dialog in all Ajax Operation 3/14/2008 6:52 PM game
very nice

# re: Showing Modal Progress Dialog in all Ajax Operation 3/14/2008 6:59 PM nameless
thnkx nice script

# re: Showing Modal Progress Dialog in all Ajax Operation 4/17/2008 2:02 AM resimler
Thank You for another very interesting article. So please try to keep up the great work all the time.

# re: Showing Modal Progress Dialog in all Ajax Operation 4/26/2008 2:36 AM nata
Hope, it is not too late to say thank you for your work!

# re: Showing Modal Progress Dialog in all Ajax Operation 5/8/2008 4:51 PM video izle
Thanks. Good article.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 3 and 6 and type the answer here: