C# – Show a Loading Message using master pages while page loads data asp.net

.net-4.0asp.netcmaster-pagespage-loading-message

I have spent hours Googling and searching past questions but have struggled to get anywhere with those answers (be it I can't get it into my solution correctly or it isn't suitable for me as they don't have master pages).

My question is, I have a couple of asp.net pages which can take a while to load (sometimes 5 seconds+) as there is a lot of data being requested from a database on the Page_Load method. To stop users from thinking that the page has crashed and either refreshing the page or doing something else, I want to put up a Loading message hiding everything else on the page (apart from the menu) while it loads.

I am using ASP.Net 4.0 with master pages and coding in C#.

The one where I get the most success is using the UpdatePanel on my master page where the content template covers the contentplaceholder, however I know this is not the best way to go about it and anyway it only shows up once, i.e. The user logs in, the loading message appears and once all the data has loaded on the home page (dashboard.aspx) the loading message disappears, which is kind of what I want. However if the user goes away from that page and then clicks home the loading message never appears again, just takes a while to load. It also never appears for any other page that takes a noticeable time to load.

Below is the body of the master.aspx

<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<div class="page">
    <div class="header">
        <div class="title">
            <h1>
                Header
            </h1>
        </div>
        <div class="loginDisplay">
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                <AnonymousTemplate>
                    [ <a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a>
                    ]
                </AnonymousTemplate>
                <LoggedInTemplate>
                    Welcome <span class="bold">
                        <asp:LoginName ID="HeadLoginName" runat="server" />
                    </span>! [
                    <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out"
                        LogoutPageUrl="~/Default.aspx" />
                    ]
                </LoggedInTemplate>
            </asp:LoginView>
        </div>
        <div class="clear hideSkiplink">
            <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"
                IncludeStyleBlock="false" Orientation="Horizontal" OnMenuItemClick="NavigationMenu_MenuItemClick">
                <Items>
                    <asp:MenuItem Text="Home" />
                    <asp:MenuItem Text="About" />
                </Items>
            </asp:Menu>
        </div>
    </div>
    <div class="main">

         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:ContentPlaceHolder ID="MainContent" runat="server" />

                <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                    <ProgressTemplate>
                        <asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
                            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/loader.gif" />
                        </asp:Panel>

                    </ProgressTemplate>
                </asp:UpdateProgress>
            </ContentTemplate>
        </asp:UpdatePanel>


         <asp:UpdatePanelAnimationExtender ID="UpdatePanel1_UpdatePanelAnimationExtender" 
             runat="server" Enabled="True" TargetControlID="UpdatePanel1">
         </asp:UpdatePanelAnimationExtender>


    </div>
    <div class="clear">
    </div>
</div>
<div class="footer">
</div>
</form>

And below is the page for the dashboard.aspx (a page which has a long loading time)

<asp:Panel ID="PanelWelcome" runat="server">
    <h1>
        Welcome
        <asp:Label ID="LabelUserName" runat="server" Text="[User Name]" />&nbsp;to your
        personal Dashboard.</h1>
    <table width="100%" cellpadding="5px">
        <tr>
            <td style="width: 70%" valign="top">
                <asp:Panel ID="Panel2" runat="server">
                    <p>
                        &nbsp;</p>
                    <h4>
                        Up and Coming </h4>
                    <br />
                    <asp:GridView ID="GridViewItin" runat="server" Width="100%" HorizontalAlign="Left"
                        OnRowDataBound="GridViewItin_RowDataBound">
                    </asp:GridView>
                </asp:Panel>
            </td>
            <td style="width: 30%">
                <asp:Panel ID="PanelProfile" runat="server">
                    <asp:ImageButton ID="ImageButtonProfile" runat="server" ImageUrl="~/Images/BlankProfile.jpg"
                        Width="150px" /><br />
                    <h4>
                        Name:</h4>
                    <asp:Label ID="LabelPARname" runat="server" Text="[Person Name]"></asp:Label>
                    <h4>
                        Company:</h4>
                    <asp:Label ID="LabelBARname" runat="server" Text="[Company Name]"></asp:Label>
                    <h4>
                        Date of Birth:</h4>
                    <asp:Label ID="LabelPARdob" runat="server" Text="[DOB]"></asp:Label><br />
                    <asp:LinkButton ID="LinkButtonProfilePage" runat="server" OnClick="LinkButtonProfilePage_Click">More details...</asp:LinkButton>
                </asp:Panel>
            </td>
        </tr>
    </table>
</asp:Panel>

Could you please show me the best way to go about it and where I am going wrong? Also how I can hide the ContentTemplate when the UpdateProgress template is showing that would be great?

Thanks.

Best Answer

Okay so I figured it out what I was doing wrong, I thought I would post what I did to hopefully help someone else who might end up with the same issues...

Basically I hadn't thought about it logically. There are controls outside the update panel such as a NavigationMenu which would never fire the update progress because they had nothing to do with the Panel! I had to add triggers to the update panel to deal with all the things that happen outside the panel.

So, in my master page I had the following code

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="NavigationMenu" />
        </Triggers>
    </asp:UpdatePanel>

    <asp:UpdateProgress ID="progress" runat="server" DynamicLayout="true" DisplayAfter="0">
            <ProgressTemplate>
                <div id="overlay">
                    <div id="modalprogress">
                        <div id="theprogress">
                            <asp:Image ID="loader" runat="server" ImageAlign="AbsMiddle" ImageUrl="~/images/loader.gif" />
                            Please wait...
                        </div>
                    </div>
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>  


     <asp:UpdatePanelAnimationExtender ID="UpdatePanel1_UpdatePanelAnimationExtender" 
         runat="server" Enabled="True" TargetControlID="UpdatePanel1">
     </asp:UpdatePanelAnimationExtender>

Hopefully that will help someone else!

Related Topic