Results 1 to 3 of 3

Thread: PageTransfer Server Control

  1. #1

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    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' />

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks, I am sure that will come in handy sometime.

  3. #3

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    No problem, I use it on my forums.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width