您的位置:首页 > 编程语言 > ASP

ASP.NET页面跳转的三种方法比较

2016-04-08 21:25 579 查看
在ASP.NET下,经常需要在页面之间跳转,下面我们来分别介绍一下关于.NET中Response.Redirect(),Sever.Execute(),Server.Transfer() 三种页面跳转的方法。

一、Response.Redirect()

优点:可以跳转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪)。

缺点:

(1)、跳转的速度慢。因为它要走2个来回(2次postback),首先是发送一个 http请求到客户端,通知需要跳转到新页面,然后客户端再发送跳转请求到服务器端。

(2)、不能跳过登录保护。

(3)、跳转后前一个页面的内容将全部清空,如A页面跳转到B页面,则A页面的数据信息将会全部丢失。所以需要用Session,Cookies,Application等对象存储页面的数据。

实例
Example that uses Redirect [C#; ASP.NET]


using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class List : Page
{
7     protected void Page_Load(object sender, EventArgs e)
{
// 实例化一个response
var response = base.Response;
// Redirect temporarily.
// Don't throw an HttpException to terminate.
response.Redirect("http://www.jb51.net", false);
}
}
}


Result of the page:

HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: http://www.jb51.net Server: Microsoft-IIS/7.0
Date: Fri, 13 Aug 2010 21:18:34 GMT
Content-Length: 144
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://www.dotnetperls.com/list">here</a>.</h2>
</body></html>


二、sever.execute

这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。
举个例子看看:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
代码如下:

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("webform2.aspx");
}


4、创建过程来返回TextBox1,TextBox2控件的值代码如下:

public string Name
{
get
{
return TextBox1.Text;
}
}
public string EMail
{
get
{
return TextBox2.Text;
}
}


5、新建一个目标页面命名为webform2
6、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:

private void Page_Load(object sender, System.EventArgs e)
{
//创建原始窗体的实例
WebForm1 wf1;
//获得实例化的句柄
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}


三、server.transfer

速度快,只需要一次postback ,但是。。。。他必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。你可以写个小程序试试:设计一个由页面一到页面二的跳 转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
.net代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebForm1 : System.Web.UI.Page
{
public string Time
{
get { return DateTime.Now.ToString(); }
}
public string TestFun()
{
return "Function of WebForm1 Called";
}
protected void Page_Load(object sender, EventArgs e)
{
Context.Items.Add("Context", "Context from Form1");
}
protected void Button1_Click(object sender, EventArgs e)
{
//this.TextBox2.Text =Request ["TextBox1"].ToString ();
Server.Transfer("WebForm2.aspx", true);//第二个参数为false时,WebForm2.aspx中不能获得TextBox1的内容
}
}


总结:

如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。
·如果要确保
HTML输出合法,请使用Response.Redirect,因为Server.Execute
或者Server.Transfer方法返回给客户端的页面包含多个<Html><body>标记,不是合法的HTML页面,在
非IE浏览器中可能会发生错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: