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

2025-05-29 0 84

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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.zzvips.com", false);

}

}

}

代码如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
HTTP/1.1 302 Found

Content-Type: text/html; charset=utf-8

Location: //www.zzvips.com

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.zzvips.com/list/index_1.htm>here</a>.</h2>

</body>

</html>

第二种方法sever.execute
这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。

举个例子看看:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
代码如下

?

1

2

3

4

5
private void Button1_Click

(object sender, System.EventArgs e)

{

Server.Transfer("webform2.aspx");

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
public string Name

{

get

{

return TextBox1.Text;

}

}

public string EMail

{

get

{

return TextBox2.Text;

}

}

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

?

1

2

3

4

5

6

7

8

9

10
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地址仍然保留的是原页面的地址!
代码如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44
<%@ 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页面跳转的方法,希望对大家的学习有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 三种asp.net页面跳转的方法 https://www.kuaiidc.com/101851.html

相关文章

发表评论
暂无评论