asp.net(c#)网页跳转七种方法小结

在asp.net下,经常需要页面的跳转,下面是具体的几种方法。跳转页面是大部编辑语言中都会有的,正面我们来分别介绍一下关于.net中response.redirect sever.execute server.transfer 三种页面跳转的方法

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

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("https://www.0133.cn", false);
}
}
}

Result of the page
复制代码 代码如下:

HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: https://www.0133.cn
Server: Microsoft-IIS/7.0
Date: Fri, 13 Aug 2010 21:18:34 GMT
Content-Length: 144
Object moved

Object moved to here.




②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" %>















.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页面,在非IE浏览器中可能会发生错误。

1.Response.Redirect("https://www.0133.cn",false);
目标页面和原页面可以在2个服务器上,可输入网址或相对路径。后面的bool值为是否停止执行当前页。
跳转向新的页面,原窗口被代替。"
浏览器中的URL为新路径。
:Response.Redirect方法导致浏览器链接到一个指定的URL。当Response.Redirect()方法被调用时,它会创建一个应答,应答头中指出了
状态代码302(表示

以上就是asp.net(c#)网页跳转七种方法小结的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程