Click here to Skip to main content
6,596,602 members and growing! (20,276 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

ASP.NET/ AJAX Page Loader Progress Bar/ Splash Screen

By Shahin__

This article shows how you can display the loading progress of your page when you deal with heavy pages.
Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:1 Oct 2006
Views:101,104
Bookmarked:60 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 3.60 Rating: 3.14 out of 5
4 votes, 28.6%
1
1 vote, 7.1%
2

3
2 votes, 14.3%
4
7 votes, 50.0%
5

Introduction:


Today, complex server controls and extensive Javascript libraries (e.g. AJAX ) are very common . They can considerably increase the size and accordingly the loading time of your page.  Unfortunately it is very hard to catch how slow your page is while you are in a development environment.  You will be surprised to see how long some users have to wait until your page is completely downloaded. Wouldn�t it be nice if you could communicate to the users to stay patient until the page and scripts are downloaded? Even in some cases you would rather hide your page until it is fully downloaded to avoid premature events when your scripts are still loading. This article shows how you can display the loading process of your page with a progress bar. Additionally you can use this as a splash window to show messages, news or ads that you want the user to see first.


The technique discussed here is not limited to ASP. NET.  If you use any other server-side scripting languages, keep on reading!

UPDATE: Live Sample 

Using the code


To make life easier, I encapsulated all the code into the LoadingNotifier class. To use the LoadingNotifier , your page  inherits LoadingNotifier class. First you initialize the loading progress bar by calling initLoader() in a server script tag i.e. <%initLoader(strMsg);%>. This method writes a Javascript to the response object and calls the Flush() method  to send  the buffered output to the client. This Javascript function adds the progress bar to the DOM and shows the page is being loaded.  From here, on each achieved milestone, you call the Notify(strProgressPercent , strLoadingStatusMsg) method of LoadingNotifier and pass in the progress percentage and the status message. Each call to the Notify method writes a Javascripts function to the Response that updates the progress bar. When the progress bar reaches 100% it automatically gets removed from the DOM. The progress bar is totally customizable and takes its stylesheet from the page Theme.  You can set a background color for the Progress bar container to hide the page until it is 100% loaded. 

 

<%@ Page Language="C#" Inherits="LoadingNotifier" Theme="Default" %>
<%
    //shahin@themorningoutline.com provided this UNDER GNU GPL 

    // For more inforamtion visit http://www.themorningoutline.com/

   
    // Page Inhertits the LoadNotifier Class 

 %>

<!DOCTYPE html >
<html>
<head runat="server">
    <title>www.themorningoutline.com ASP.NET Sample</title>
</head>
<body style="background-color: #999999; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;">
  
  
    <% //Initilize thr Progress Bar and show a message

        initNotify("Welcome to theMorningOutline. Free tickets from today!");
        System.Threading.Thread.Sleep(3000);
    %>
    <form id="form1" runat="server">
  
        <span style="font-size: 12pt"><strong><span style="color: #ffffff">Select Your Departure
            Date: </span></strong> </span>
          
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <% 
            // We have achieved a milestone. Let the user know!

            Notify("30", "Loading Departure Calendar Completed ...");
            // Simulate Internet dalay

           System.Threading.Thread.Sleep(2000);
           
             %>
        <span style="font-size: 12pt"><span style="color: #ffffff"><strong>Select Your Return
            Date: </strong><span style="color: #000000"> </span></span></span>
        <asp:Calendar ID="Calendar2" runat="server"></asp:Calendar>
        <br />
      
           <% 
           Notify("60", "Loading Arrival Calendar Completed ...");
           System.Threading.Thread.Sleep(2000);
           
             %>
               <span style="font-size: 14pt"><span style="color: #ffffff">Your Recent Flights:</span><strong><span
            style="color: #ffffff"> </span></strong></span>  <br />
        
        <span style="font-size: 12pt"><span style="color: #ffffff"></span>
        </span>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataSourceID="XmlDataSource" ForeColor="#333333" GridLines="None" Style="border-left-color: gray;
            border-bottom-color: gray; border-top-style: outset; border-top-color: gray;
            border-right-style: outset; border-left-style: outset; border-right-color: gray;
            border-bottom-style: outset">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="Origin" HeaderText="Origin" SortExpression="Origin" />
                <asp:BoundField DataField="Destination" HeaderText="Destination" SortExpression="Destination" />
                <asp:BoundField DataField="Duration" HeaderText="Duration" SortExpression="Duration" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <asp:XmlDataSource ID="XmlDataSource" runat="server" DataFile="~/Source.xml" XPath="/travel/Itinerary">
        </asp:XmlDataSource>
        <%
            Notify("100", "Loading Your Previous Trips Completed...");
          System.Threading.Thread.Sleep(2000);
         %>
    </form>
     
    
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

// shahin@themorningoutline.com provided this UNDER GNU GPL 

//For more inforamtion visit www.themorningoutline.com

public class LoadingNotifier: System.Web.UI.Page
{
 
    public  void initNotify( string StrSplash)
    {
        // Only do this on the first call to the page

        if ((!IsCallback) && (!IsPostBack))
        {
            //Register loadingNotifier.js for showing the Progress Bar

            Response.Write(string.Format(@"<script type='text/javascript' src='scripts/loadingNotifier.js'></script>
              <script language="'javascript'" type='text/javascript'>
              initLoader('{0}');
              </script>",StrSplash));
           // Send it to the client

            Response.Flush();
            
        }

    }
    public  void Notify(string strPercent, string strMessage)
    {
        // Only do this on the first call to the page

        if ((!IsCallback) && (!IsPostBack))
        {
            //Update the Progress bar


 
           Response.Write(string.Format("<script language="'javascript'" type='text/javascript'>setProgress({0},'{1}'); </script>"
, strPercent, strMessage));
            Response.Flush();
            
        }

    }
   
}

 

Remember that the Page_Load event has been processed before the page is being downloaded therefore if you have expensive operations at your Page_Load event, you cannot display the processing progress using this method. This method uses W3C Standard DOM methods which require the body tag to be present to display the progress bar. To display the loading status during Page_Load or other events which are processed prior to sending the page to the client, you can directly write your status message to the Response object and call the Flush method. When the page is completed at the client, you should clean up your status messages.


Conlusion


Finally remember that using the LoadingNotifier class actually slows down the loading process of your page and it should be avoided unless your page is really heavy. comments here!

Cheers!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Shahin__


Member

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralLoading Gridview Progress Pinmemberkjward5:21 16 Oct '07  
QuestionHelp "Response is not available in this context. " [modified] Pinmemberrafiki31@gmail.com8:14 15 Aug '07  
GeneralRe: Help "Response is not available in this context. " Pinmembermohit rawat1:28 19 Feb '08  
Generalcode behind Pinmemberdsmportal13:52 7 Apr '07  
GeneralRe: code behind Pinmemberdsmportal5:56 8 Apr '07  
QuestionUsing Progress Bar with Master pages / within form or div tag Pinmemberandre27116:45 22 Jan '07  
AnswerRe: Using Progress Bar with Master pages / within form or div tag Pinmemberjuras99:47 24 Apr '07  
GeneralRe: Using Progress Bar with Master pages / within form or div tag PinmemberDaniel Ballinger15:38 28 Aug '07  
Generalphp loader Pinmemberjonavanmona12:40 5 Nov '06  
GeneralExtended CSS used (off topic) Pinmemberpharaon_login14:07 7 Oct '06  
AnswerRe: Extended CSS used (off topic) [modified] PinmemberShahin__7:56 8 Oct '06  
QuestionDisplay the loading status during Page_Load Pinmembersithwad1:04 3 Oct '06  
AnswerRe: Display the loading status during Page_Load PinmemberShahin__16:42 3 Oct '06  
GeneralRe: Display the loading status during Page_Load Pinmembersithwad23:29 3 Oct '06  
GeneralRe: Display the loading status during Page_Load PinmemberShahin__18:53 4 Oct '06  
Generalgreat job Pinmembervik2019:58 2 Oct '06  
QuestionASP.Net with VB.Net version PinmemberCatMark22:30 1 Oct '06  
AnswerRe: ASP.Net with VB.Net version PinmemberShahin__9:55 2 Oct '06  
AnswerRe: ASP.Net with VB.Net version Pinmemberdacoty10:24 4 Oct '06  
GeneralRe: ASP.Net with VB.Net version PinmemberShahin__18:38 4 Oct '06  
GeneralRe: ASP.Net with VB.Net version Pinmemberdacoty3:45 5 Oct '06  
QuestionRe: ASP.Net with VB.Net version Pinmemberdavidswift7:29 16 Aug '07  
AnswerRe: ASP.Net with VB.Net version Pinmemberdacoty10:09 1 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Oct 2006
Editor:
Copyright 2006 by Shahin__
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project