-[Danny Chen]- Blog of an ASP.NET QA tester

Tips and info about Site Navigation, ImageMap, Menu and other cool ASP.NET v2.0 features.

Custom Control Challenge - Entry #1

Credit goes to:  crms -at- sina.com

 
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestCode
{
    [
ToolboxData("<{0}:WaitCursor runat=server></{0}:WaitCursor>")]
    
public class WaitCursor : Control
    {
        
protected override void OnInit(EventArgs e)
        {
            
//only allow one waiting for each page
            if (HttpContext.Current.Items["Waiting"] == null)
            {
                Page.Response.Write(
@"<div id='myid'

style='width:100%;height:100%;z-index:9999;background-color:white'>loading...</div>"
);
                Page.Response.Write(
@"<script language='javascript'>
var c = document.getElementsByName('myid')[0];
c.style.display='';
var t = null;
var m = function()
{
if(document.readyState == 'complete')
{
c.style.display = 'none';
clearInterval(t);
}
}

//maybe we should make the interval configurable

t = setInterval(m,100);
</script>
"
);

                Page.Response.Flush();

                
HttpContext.Current.Items["Waiting"] = "yes";
            }
        }
    }
}
Posted: Mar 02 2006, 09:24 AM by dannychen | with 1 comment(s)
Filed under:

Comments

crms said:

actually, I was thinking, if a label is all you need, we can spare the timer and output the part which hides the label in WaitCursor's Render method

but a timer solution will be interesting if you are doing some animation when waiting...


protected override void OnInit(EventArgs e)
{
//only allow one waiting for each page
if (HttpContext.Current.Items["Waiting"] == null)
{
Page.Response.Write(@"<div id='myid' style='width:100%;height:100%;z-index:9999;background-color:white'>loading...</div>");

Page.Response.Flush();

HttpContext.Current.Items["Waiting"] = "yes";
}
}

protected override void Render(HtmlTextWriter writer)
{
if (HttpContext.Current.Items["Waiting"] != null)
{
Page.Response.Write(@"<script language='javascript'>
var c = document.getElementsByName('myid')[0];
c.style.display='none';
</script>
");
HttpContext.Current.Items["Waiting"] = null;
}
}
# March 2, 2006 1:21 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)