本文實例為大家分享了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("http://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 SignalR創建實時聊天應用程序
- Asp.NET MVC中使用SignalR實現推送功能
- asp.net mvc signalr簡單聊天室制作過程分析
- Asp.net使用SignalR實現消息提醒
- Asp.net SignalR支持的平臺有哪些
- Asp.net使用SignalR實現發送圖片
- ASP.net實現頁面跳轉的方法
- ASP.NET筆記之頁面跳轉、調試、form表單、viewstate、cookie的使用說明
- 新發現原來documenet.URL也可以實現頁面跳轉
- SignalR發送頁面跳轉通知的方法