How to display loading text whilst processing

Occasionally when writing ASP.NET pages you may find that the Page_Load subroutine will take a long time to complete and therefore the final page will not be viewable by the end user until the processing has completed. It is possible to send some HTML to render before the Page_Load subroutine has finished using the Response.Write and Response.Flush commands.

The Response.Write command is used to write the HTML that you wish the browser at the user end to render, this could be either a simply loading text or something more complex, once all of the required HTML has been queued up using multiple Response.Write commands then you can call the Response.Flush command to send the HTML written so far to the end user browser, the Page_Load subroutine can then continue processing as before.

Response.Write("<div id=""wait"" align=""center"" style=""width:100%;height:100%;"">Loading text</div>")
Response.Flush()

Once the Page_Load subroutine has completed then the final HTML will be sent to the end users browser to render, the previously sent loading text will still be present so we will need a way of hiding the loading text. The simplest way is to encompass the loading text within a <div> which has been given an pre-assigned id such as ‘wait’. Then within the body onload command we can hide the <div> by using the pre-assigned id.

<script type="text/javascript" language="javascript">
<!--
   function hidewait() {
      wait.style.display = "none";
   }
-->
</script>
<body onload="hidewait();" class="summary">

This wil have the effect of displaying the loading text on screen until the page has finally loaded and then will hide the loading text leaving the final HTML on.

Whilst I was working on this I found an issue with the rendering of this in Internet Explorer, it would work in all other browsers except Internet Explorer. It turns out the IE will not render the HTML if the content is less than 256 characters in length. As such the following code was created:

Public Function GetResponseFlush(ByVal Text As String) As String
   GetResponseFlush = Text
   Dim TextLength As Integer = Text.Length
   For LengthCnt As Integer = TextLength To 256
      GetResponseFlush += " "
   Next
End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.