PageTransfer Server Control
Thought I would share a server control I just developed for a project I'm working on. When you log into vbforums, you are presented to a page that displays a message and then transfers you to another page after 2 seconds or so. This simple control allows you to set the timeout and destination url. Here's the class:
Code:
using System;
using System.Text;
using System.Web.UI;
namespace VegaControlsLib {
public class PageTransfer : Control {
#region Protected Overrides
/// <summary>
/// Registers the client script block
/// </summary>
protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (TransferUrl != null) {
string scriptKey = typeof(PageTransfer).FullName;
Page.RegisterClientScriptBlock(scriptKey, GetClientScriptBlock());
}
}
#endregion
#region Private Methods
/// <summary>
/// Builds the necessary script block used for the transfer
/// </summary>
/// <returns>Script Block</returns>
private string GetClientScriptBlock() {
StringBuilder script = new StringBuilder();
script.Append("<script language='JavaScript'>");
script.Append("\n\t");
script.Append("<!--");
script.Append("\n\t");
script.Append(String.Format("setTimeout('window.location=\"{0}\"', '{1}')", TransferUrl, Timeout));
script.Append("\n\t");
script.Append("//-->");
script.Append("\n\t");
script.Append("</script>");
return script.ToString();
}
#endregion
#region Public Properties
/// <summary>
/// Gets and sets the transfer url
/// </summary>
private string _transferUrl;
public string TransferUrl {
get { return _transferUrl; }
set {
_transferUrl = value;
}
}
/// <summary>
/// Gets and sets the timeout
/// </summary>
private int _timeout = 2500;
public int Timeout {
get { return _timeout; }
set {
_timeout = value;
}
}
#endregion
}
}
Next, you need to register the control on your page by adding this to the top of your aspx page:
Code:
<%@ Register TagPrefix="VegaControls" Namespace="VegaControlsLib" Assembly="VegaControlsLib" %>
// ie..
<VegaControls:PageTransfer runat="server" TransferUrl="http://www.google.com" Timeout='5000' />