How To: Redirect a user to a page using popups

Source.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Source.aspx.cs" Inherits="Source" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:HyperLink ID="hlRedirect1" runat="server" NavigateUrl="RedirectToSite.aspx?site=1">Redirect 1</asp:HyperLink><br />
          <asp:HyperLink ID="hlRedirect2" runat="server" NavigateUrl="RedirectToSite.aspx?site=2">Redirect 2</asp:HyperLink><br />
          <asp:HyperLink ID="hlRedirect3" runat="server" NavigateUrl="RedirectToSite.aspx">Redirect 3</asp:HyperLink>&nbsp;</div>
  </form>
</body>
</html>
RedirectToSite.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RedirectToSite.aspx.cs" Inherits="RedirectToSite" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
   <title>Redirect to site</title>
   <script type="text/javascript">
   <!--
       function popup(mylink, windowname)
       {
       if (! window.focus)return true;
       var href;
       if (typeof(mylink) == 'string')
          href=mylink;
       else
          href=mylink.href;
       window.open(href, windowname, 'width=800,height=600,scrollbars=yes');
       return false;
       }
   //-->
   </script>
</head>
<body onload="<%= redirectionEvent %>" >
   <form id="form1" runat="server">
   <div>
       <asp:Label ID="lblRedirectStatus" runat="server" Text="Unknown site..."></asp:Label></div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class RedirectToSite : System.Web.UI.Page
{
    protected string redirectionEvent = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //our popup call
        string redirectionInfo = "popup('@popurl', 'ad')";
        //the popup url
        string popurl = "";
        //check whether there is a value for our querystring
        if (Request.QueryString["site"] != null)
        {
            //get the siteid
            string siteId = "";
            siteId = Request.QueryString["site"].ToString();

            //if siteid is one then...
            if (siteId == "1") { popurl = "http://www.google.com"; }
            //if siteid is two then
            else if (siteId == "2") { popurl = "http://www.blogger.com"; }

            //display our redirect information
            lblRedirectStatus.Text = "Redirecting to " + popurl;
            //replace the @popurl placeholder with the popurl value
            redirectionEvent = redirectionInfo.Replace("@popurl", popurl);
        }
    }
}

No comments:

Post a Comment