ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Reload Refresh and Redirect Pages using Meta Tags in ASP.Net
Author Name: Mudassar Khan Published Date: June 10, 2009
Filed Under :
ASP.Net
Views: 968

In this article, I am explaining how we can achieve refreshing / reloading of pages and delayed redirect using Meta tags in ASP.Net

 

Refresh / Reload Page at specific intervals

In order to reload or refresh a page at regular intervals of time you can place the following Meta tag in the head section of the ASP.Net Web Page

<head runat="server">

    <title>Meta Tags Example</title>

     <meta http-equiv="Refresh" content="5" />

</head>

 

The above meta tag refreshes or reloads the page every 5 seconds automatically

If you want to set it from code behind refer below


Method 1

C#

protected void Page_Load(object sender, EventArgs e)

{

    HtmlMeta meta = new HtmlMeta();

    meta.HttpEquiv = "Refresh";

    meta.Content = "5";

    this.Page.Header.Controls.Add(meta);   

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     Dim meta As New HtmlMeta()

     meta.HttpEquiv = "Refresh"

     meta.Content = "5"

     Me.Page.Header.Controls.Add(meta)

End Sub

 

 

   

Method 2

Shorter way of doing the above is appending it to the Response

C#

protected void Page_Load(object sender, EventArgs e)

{

    Response.AppendHeader("Refresh", "5");

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Response.AppendHeader("Refresh", "5")

End Sub

 

The only difference between above two methods is that if you do it with method 1 you can physically view the added meta tag in the HTML Source of the page while in method 2 you cannot see it still both methods perform the same way

 

While using the method 1 you might get the following error

Server Error in 'ASP.Net' Application.

 

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 

For the solution refer my article

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 



Delayed Redirection

There are situations when you want to redirect the user to a certain page after certain amount of time or interval in other words delayed redirection your asp.net web application. In that case you can take help of the meta tags.

<head runat="server">

    <title>Meta Tags Example</title>

    <meta http-equiv="Refresh" content="5;url=Page2.aspx" />

</head>

 

From code behind you will have to choose any of the two methods

Method 1

 

C#

protected void Page_Load(object sender, EventArgs e)

{

    HtmlMeta meta = new HtmlMeta();

    meta.HttpEquiv = "Refresh";

    meta.Content = "5;url=Page2.aspx";

    this.Page.Controls.Add(meta);  

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     Dim meta As New HtmlMeta()

     meta.HttpEquiv = "Refresh"

     meta.Content = "5;url=Page2.aspx"

     Me.Page.Header.Controls.Add(meta)

End Sub

 

Method 2

    

 

C#

protected void Page_Load(object sender, EventArgs e)

{

    Response.AppendHeader("Refresh", "5;url=Page2.aspx");

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Response.AppendHeader("Refresh", "5;url=Page2.aspx")

End Sub

 



Automatic Redirect on Session Timeout

This one is great use of the meta tags that is redirection of user to Login Page or Session Timeout page or Logout page automatically on Session Timeouts. As you will notice below I simply convert the Timeout to seconds by multiplying by 60 and then setting it in the meta tag. Thus as soon as timeout occurs user is redirected to the Logout or Login page that you set in the URL parameter.

 

Method 1

 

C#

protected void Page_Load(object sender, EventArgs e)

{

    HtmlMeta meta = new HtmlMeta();

    meta.HttpEquiv = "Refresh";

    meta.Content = Convert.ToString(Session.Timeout * 60) + ";url=LogOut.aspx";

    this.Page.Header.Controls.Add(meta);     

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      Dim meta As New HtmlMeta()

      meta.HttpEquiv = "Refresh"

      meta.Content = Convert.ToString(Session.Timeout * 60) & ";url=LogOut.aspx"

      Me.Page.Header.Controls.Add(meta)

End Sub

 

 

Method 2

 

C#

protected void Page_Load(object sender, EventArgs e)

{

    Response.AppendHeader("Refresh", Convert.ToString(Session.Timeout * 60)

                           + ";url=LogOut.aspx");  

}

 

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     Response.AppendHeader("Refresh", Convert.ToString(Session.Timeout * 60) _

                              & ";url=LogOut.aspx")

End Sub

 

With this the article comes to end in which I tried to put up some of the usefulness of meta tags in ASP.Net applications

 


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code