三种asp.net页面跳转的方法

所属分类: 网络编程 / ASP.NET 阅读数: 1621
收藏 0 赞 0 分享

本文实例为大家分享了asp.net页面跳转的三种方法,供大家参考,具体内容如下

第一种方法:response.redirect

这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但它可以跳转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session。

代码如下 

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

代码如下 

HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: //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=//www.jb51.net/list/index_1.htm>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浏览器中可能会发生错误。

以上就三种asp.net页面跳转的方法,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多