Click here to Skip to main content
15,171,993 members
Articles / Web Development / ASP.NET
Posted 1 Oct 2006


73 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.12/5 (18 votes)
1 Oct 2006CPOL2 min read
This article shows how you can display the loading progress of your page when you deal with heavy pages.


Today, complex server controls and extensive JavaScript libraries (e.g., AJAX ) are very common. They can considerably increase the size and the loading time of your page. Unfortunately, it is very hard to catch how slow your page is while you are in the 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? 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 are using 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 the LoadingNotifier class. First, you initialize the loading progress bar by calling initLoader() in a server script tag, e.g.: <%initLoader(strMsg);%>. This method writes 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 JavaScript 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" %>
    // provided this UNDER GNU GPL 
    // For more inforamtion visit
    // Page Inhertits the LoadNotifier Class 

<!DOCTYPE html >
<head runat="server">
    <title> ASP.NET Sample</title>
<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!");
    <form id="form1" runat="server">
        <span style="font-size: 12pt"><strong>
          <span style="color: #ffffff">Select Your Departure Date: </span></strong> 
        <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
        <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 ...");
               <span style="font-size: 14pt">
               <span style="color: #ffffff">Your Recent Flights:</span><strong>
               <spanstyle="color: #ffffff"> </span></strong></span>  <br />
        <span style="font-size: 12pt"><span style="color: #ffffff"></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" />
                <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" />
            <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:XmlDataSource ID="XmlDataSource" runat="server" 
                  DataFile="~/Source.xml" XPath="/travel/Itinerary">
            Notify("100", "Loading Your Previous Trips Completed...");

The code-behind:

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;

// provided this UNDER GNU GPL 
//For more inforamtion visit
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' 
              <script language="'javascript'" type='text/javascript'>
           // Send it to the client

    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));

Remember that the Page_Load event has been processed before the page is being downloaded, therefore if you have expensive operations in your Page_Load event, you cannot display the processing progress using this method. This method uses the 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.


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!



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionLinks are all obsolete Pin
RJ_Zed24-Jun-16 2:57
MemberRJ_Zed24-Jun-16 2:57 
QuestionLive Demo Site Pin
david.ve3ofa4-Mar-14 11:50
Memberdavid.ve3ofa4-Mar-14 11:50 
GeneralMy vote of 1 Pin
lalupatel25-Nov-10 21:53
Memberlalupatel25-Nov-10 21:53 
GeneralHii :) Pin
Navin Pandit16-Dec-09 23:55
MemberNavin Pandit16-Dec-09 23:55 
GeneralLoading Gridview Progress Pin
kjward16-Oct-07 5:21
Memberkjward16-Oct-07 5:21 
QuestionHelp &quot;Response is not available in this context. &quot; [modified] Pin
rafiki31@gmail.com15-Aug-07 8:14
Memberrafiki31@gmail.com15-Aug-07 8:14 
GeneralRe: Help &quot;Response is not available in this context. &quot; Pin
mohit rawat19-Feb-08 1:28
Membermohit rawat19-Feb-08 1:28 
Generalcode behind Pin
dsmportal7-Apr-07 13:52
Memberdsmportal7-Apr-07 13:52 
GeneralRe: code behind Pin
dsmportal8-Apr-07 5:56
Memberdsmportal8-Apr-07 5:56 
QuestionUsing Progress Bar with Master pages / within form or div tag Pin
andre271122-Jan-07 6:45
Memberandre271122-Jan-07 6:45 
AnswerRe: Using Progress Bar with Master pages / within form or div tag Pin
juras924-Apr-07 9:47
Memberjuras924-Apr-07 9:47 
GeneralRe: Using Progress Bar with Master pages / within form or div tag Pin
Daniel Ballinger28-Aug-07 15:38
MemberDaniel Ballinger28-Aug-07 15:38 
Generalphp loader Pin
jonavanmona5-Nov-06 12:40
Memberjonavanmona5-Nov-06 12:40 
GeneralExtended CSS used (off topic) Pin
Sergey Rybalkin7-Oct-06 14:07
MemberSergey Rybalkin7-Oct-06 14:07 
AnswerRe: Extended CSS used (off topic) [modified] Pin
Shahin__8-Oct-06 7:56
MemberShahin__8-Oct-06 7:56 
QuestionDisplay the loading status during Page_Load Pin
sithwad3-Oct-06 1:04
Membersithwad3-Oct-06 1:04 
AnswerRe: Display the loading status during Page_Load Pin
Shahin__3-Oct-06 16:42
MemberShahin__3-Oct-06 16:42 
GeneralRe: Display the loading status during Page_Load Pin
sithwad3-Oct-06 23:29
Membersithwad3-Oct-06 23:29 
GeneralRe: Display the loading status during Page_Load Pin
Shahin__4-Oct-06 18:53
MemberShahin__4-Oct-06 18:53 
Generalgreat job Pin
vik202-Oct-06 19:58
Membervik202-Oct-06 19:58 
QuestionASP.Net with VB.Net version Pin
Markgina1-Oct-06 22:30
MemberMarkgina1-Oct-06 22:30 
AnswerRe: ASP.Net with VB.Net version Pin
Shahin__2-Oct-06 9:55
MemberShahin__2-Oct-06 9:55 
AnswerRe: ASP.Net with VB.Net version Pin
dacoty4-Oct-06 10:24
Memberdacoty4-Oct-06 10:24 
GeneralRe: ASP.Net with VB.Net version Pin
Shahin__4-Oct-06 18:38
MemberShahin__4-Oct-06 18:38 
GeneralRe: ASP.Net with VB.Net version Pin
dacoty5-Oct-06 3:45
Memberdacoty5-Oct-06 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.