The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Adding Real-Time Status to ASP.NET Pages

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Doug Thews

Posts: 866
Nickname: dougthews
Registered: Jul, 2003

Doug Thews is a software developer/manager for D&D Consulting Services with 18+ years of experience
Adding Real-Time Status to ASP.NET Pages Posted: Jul 22, 2003 2:41 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Doug Thews.
Original Post: Adding Real-Time Status to ASP.NET Pages
Feed Title: IlluminatiLand
Feed URL: http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=JETBRAINS.COM
Feed Description: A technology blog for people enlightened enough to think for themselves
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Doug Thews
Latest Posts From IlluminatiLand

Advertisement
When I design ASP.NET pages, I try to think of ways to let the user know what's happening.  Nothing's worse than setting there watching the IE flag waving and wondering what's happening.  Am I stuck, is my link up, did the program crash, should I try again?

It's wishful thinking to assume that every page is going to provide sub-second response. For example, on my LocalDesktop project, I provided a cache reset mechanism which involved going out and retrieving as many RSS feeds as specified in the configuration file.  So, rather than making the user guess, I provided real-time status updates to the browser.

How did I do that? Well, that's what this blog entry is all about.  The first thing to do is to create some kind of generic HTML control to hold the status (like a
) .  Once created, make sure it has the runat="server" attribute and a unique id attribute so that it can be accessed by the code-behind file.  Mine looks something like this:
  


One thing that the VS.NET IDE does not do for you here is provide the control definition, like it does for ASP.NET controls.  So, we'll have to do this manually.  Go into the codebehind file, and place the following definition to a generic HTML control where other ASP.NET controls would normally be:
  Protected WithEvents pagestats As _
    System.Web.UI.HtmlControls.HtmlGenericControl


The next thing we need to do is override the page's Render() method so that we can do our processing here. If we do it before the render, then we don't have the opportunity to force status out at-will.  Within the Render() method, we can check to what operation is causing a postback, so we know what operations to do.  Our event handlers that require real-time status, will merely set flags for the Render() method to react to.  Since we all write modular code (right?  :), it should be pretty easy to make calls to our business logic functions from within Render() instead of our event handlers.  Keep in mind that this is only necessary for those events that require real-time status (which shouldn't be every one).

OK, so here's what a sample Render() method would look like that has 2 sections of code that just wait for 10 seconds (which is where a normally long running operation requiring real-time status would be).  Our single button event handler just sets the flag boolStartEvent for us.

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
' Call the parent render and then flush the contents to get them to the browser quickly
MyBase.Render(writer)
writer.Flush()
Response.Flush()

' Check to see if btnClear was clicked and that's why we're rendering
If boolStartEvent Then

' Send a "starting operation" message
WriteStatusUpdate(writer, "Waiting for 10 seconds ...", False)

' Do your long running operation here - provide status at will
Dim TimerEnd As Long = Now().Ticks + 100000000
While Now().Ticks <= TimerEnd
End While

' Send a "completed operation" message
WriteStatusUpdate(writer, "Done", True)

' Send a "starting operation" message
WriteStatusUpdate(writer, "Waiting for another 10 seconds ...", False)

' Do your long running operation here - provide status at will
TimerEnd = Now().Ticks + 100000000
While Now().Ticks <= TimerEnd
End While

' Send a "completed operation" message
WriteStatusUpdate(writer, "Done", True)

' Clear flag
boolStartEvent = False
End If

End Sub


The first thing our overridden Render needs to do is make a call back to the MyBase.Render() method so that all of the child controls and other HTML get rendered. Since there's no long-running business logic (anymore), this should be a quick render.

So, what's this function WriteStatusUpdate? Well, its one of my common library functions that I use.  What it does is use the Writer object of the Render() method to push out new HTML to the innerHTML attribute of the status object (in this case the
).  We push it out in a complete JavaScript tag so that it gets rendered by the browser immediately, instead of waiting for the ending HTML tag.

Private Sub WriteStatusUpdate(ByVal writer As System.Web.UI.HtmlTextWriter,
_ByVal Status As String, ByVal NewLine As Boolean)
If NewLine Then
writer.Write _
(String.Format("", Status))
Else
writer.Write _
(String.Format("", Status))
End If
writer.Flush()
Response.Flush()
End Sub


Notice that we do both a writer.Flush() and a Response.flush() after inserting our status message. This is because we need the writer to flush all the contents of the stream to as a response, but then we need to flush the response to the client's browser so it'll actually get rendered real-time.

The text that you send to WriteStatusUpdate can contain embedded HTML (with the exception of and ), as long as it is balanced.  This means that you can't have an open tag across multiple calls to the WriteStatusUpdate function. This is because we're flushing the contents to the browser, so it doesn't remember anything about the previous HTML statements since it's already rendered them.

So now, with just a little bit of added code, you can provide real-time status (when necessary) for your ASP.NET web applications.  Enjoy!

Read: Adding Real-Time Status to ASP.NET Pages

Topic: David Bowens Tears ACL Previous Topic   Next Topic Topic: CF.NET Working Directories & Paths

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use