Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

ASP.NET Custom Control: WebRequestPanel

3.37/5 (13 votes)
17 Nov 2006CPOL3 min read 1   554  
This is an article on an ASP.NET custom control to display any text (like Loading...) when the page is requesting data from a web server.

Introduction

This is an ASP.NET custom control that displays a text and/or image when the browser is requesting data from the server. It has a lot of options to set backcolor, forecolor, font, position, etc. To use this control, all you have to do is drag drop this control onto the ASPX page in which you want to add this functionality.

Using the code

You can find some of the properties that are exposed in this control listed below. These doesn't include properties that are inherited from System.Web.UI.WebControls.WebControl.

Properties

  • Left - Left position of the control
  • Top - Top position of the control
  • StyleSheet - Stylesheet to use when a CSS class is provided
  • ContentType - Content type. It could be 'Text', 'Image', or 'Text N Image'; defaults to 'Text'
  • Text - Text to display, if content type is 'Text' or 'Text N Image'; defaults to 'Loading.....'
  • ImageURL - URL of the image to display, if content type is 'Image' or 'Text N Image'
  • DisplayWhenNewSession - This property tells whether to display the text when a new session is created; when it is set to false, it will not display the text when a new session has been created during the postback; if you get an HttpException (0x80004005), set this property to false; it defaults to false

Let us see an example

We will see two examples. One with backcolor, forecolor, etc., added to the control directly through its properties, and another with these things specified in a CssClass.

Example 1

In this example, all the backcolor, forecolor, font, etc., are defined through the properties exposed by the control.

Below is the HTML that you have to put in the ASPX page.

HTML
<cc1:WebRequestPanel id=WebRequestPanel1 
    Width="100px" Height="15px" Font-Size="Smaller" 
    Font-Names="Verdana" ForeColor="white" BackColor="red" 
    runat="server" Text=" Loading... "></cc1:WebRequestPanel>

The page with the above control is as below:

WebRequestPanel Control

Example 2

In this example, all the backcolor, forecolor, font, etc., are defined in a CssClass. You set the style sheet to use in the StyleSheet property so that the control knows which stylesheet to use. You might be wondering why you have to set the StyleSheet property. This is because the control flushes a table to the client during its Oninit; during this, the style sheet you had linked with the page would not be sent to the client so we have to send the style sheet to the client from the control.

Below is the HTML that you have to put in the ASPX page:

HTML
<cc1:WebRequestPanel Text=" Loading... " id="WebRequestPanel1" 
    runat="server" CssClass="WebRequestPanel" 
    StyleSheet="~/Style/StyleSheet.css"></cc1:WebRequestPanel>

The page with the above control is as below:

WebRequestPanel Control

Limitation

  1. You should not be using this control in pages where you might add or modify your HTTP response headers. If you modify or add any HTTP response header in your code, you will get an error saying "Server cannot append header after HTTP headers have been sent."

Points of interest

  1. If you get an Exception saying "Session state has created a session ID, but cannot save it because the response was already flushed by the application", then make sure you have set DisplayWhenNewSession to 'false'.
  2. If you want to set any property, set it in the HTML itself. Because the control's HTML would be flushed to the client before the page Init is called, which is the first chance you might get to change your properties.
  3. If you want to use this control in ASP.NET 1.1, all you have to do is download the source and copy WebRequestPanel.vb into a .NET 1.1 Class Library. Now add the required references and compile the class library.
  4. If you go to a page which doesn't have a WebRequestPanel from the page which has the WebRequestPanel, it will be visible in the redirected page too; to fix this problem, add the panel to the redirected page and set its DontDisplay property to true. Make sure you have the same name for the control as you had in the previous page.

History

  • 2006-08-24 (YYYY-MM-DD): Article created.
  • 2006-11-09 (YYYY-MM-DD): "Always Shows" issue fix.

License

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