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