妙用HtmlMeta的HttpEquiv属性进行跨网页转向及值传递

来源:互联网 发布:矩阵乘法 r语言 编辑:程序博客网 时间:2024/05/18 01:49

原由:

现已写成生成新闻的一个动态页面d.aspx,其中可以传入多个参数:比如关键字(kw),页码(pn),最大页数(mpn),是否单页生成(isOne),文件文件所放置的目录(dir)等。其形式可能为:

d.aspx?pn=XX&mpn=XX&dir=XX&isOne=XX&kw=XX
(注:pn=XX中的XX为页数,比如第一页为1,第二页为2……)

 

由于d.aspx页面中,有自动重定向功能,比如生成本页之后,再继续生成下一页,则window.location.href可能就是:d.aspx?pn=(XX+1)&mpn=XX&dir=XX&isOne=XX&kw=XX

注:pn=(XX+1)中的XX+1表示为上一次的页码数加1。

 

由于入口页面(g.aspx)仅作为生成参数用,之后真正实施处理动作的页面为d.aspx,而且d.aspx页面有自动重定向功能,因此,如何跨页面转值就成了一个问题。

 

讲述:


我们知道,跨页面转值有很多种方式,比如:


(1)使用QueryString变量,然后Response.Redirect(url):

比如:string url; 
    url = "b.aspx?name=" + Label1.Text;
    Response.Redirect(url);


(2)使用Application 对象变量或使用Session变量甚至Cookie对象变量
Application范围是全局,对所有用户都有效,显然这里不合适。
Session变量与Application类似,Session, Cookie都作用于用户个人。这里可用,但没有必要。我这里的一个主要原因是处理页面d.aspx中没有使用Session,Cookie方式读取。

(3)使用Server.Transfer方法
(4)使用Server.Excute方法
(5)使用.net2.0的Cross-Page Posting跨页面提交技术
经测试,上面(3),(4),(5)都不适合于目标页面再自我自动重定向。所以,看来得另辟新径。

 

于是,我想到了使用javascript中的技术,给window.location.href赋值。大致代码是:
        if (IsPostBack && !IsStartupScriptRegistered("Startup"))
        {
            string newUrl = @"d.aspx?pn=" + pn.Text + "&mpn=" + mpn.Text + "&dir=" + dir.Text + "&isOne=" + isOne.Checked + "&kw=" + HttpUtility.UrlEncode(kw.Text,

System.Text.Encoding.GetEncoding("UTF-8"));
           
            String scriptString = @"<script language=JavaScript>";
            scriptString += @"top.window.location.href='";
            scriptString += newUrl;
            scriptString += @"';<";
            scriptString += @"/";
            scriptString += "script>";
            RegisterStartupScript("Startup", scriptString);

        }

测试了一下,都不好用,主要是d.aspx并不自动自我重定向进行下一页处理。

 

最后想到了Meta,嘿嘿,回到了HTML时代~~

先看看HTML:
<meta http-equiv="refresh" content="5" />,这个五秒钟后自己刷自己一把,两把……直到关掉浏览器为止。
5秒之后转到某个页面:
<meta http-equiv="refresh" content="5; url=http://www.brawdraw.com/second.htm" />
对于asp.net呢,就是HtmlMeta的HttpEquiv属性了。
//g.aspx文件(入口页面)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="G.aspx.cs" Inherits="G" EnableViewState="false" %>
<html>
<head runat="server" id="gHeader">
    <title>生成新闻</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        关键字:<asp:TextBox ID="kw" runat="server"></asp:TextBox>
        页码:<asp:TextBox ID="pn" runat="server" Width="56px">1</asp:TextBox>&nbsp; 最大页数:<asp:TextBox
            ID="mpn" runat="server" Width="63px">8</asp:TextBox>&nbsp;<asp:CheckBox ID="isOne"
                runat="server" Text="仅生成单页" /><br />
        生成文件目录:<asp:TextBox ID="dir" runat="server" Width="475px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="开始生成文件" />
        </div>
    </form>
</body>
</html>

// g.aspx.cs

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 G : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 string newUrl = @"d.aspx?pn=" + pn.Text + "&mpn=" + mpn.Text + "&dir=" + dir.Text + "&isOne=" + isOne.Checked + "&kw=" + HttpUtility.UrlEncode(kw.Text,  

System.Text.Encoding.GetEncoding("UTF-8"));
 if (IsPostBack)
 {
  HtmlMeta meta = new HtmlMeta();
  meta.HttpEquiv  = "refresh";
  meta.Content = @"0; url=" + newUrl;
  gHeader.Controls.Add(meta);
 }
    }
}


OK。

原创粉丝点击