三種asp.net頁面跳轉的方法_實用技巧

來源:互聯網
上載者:User

第一種方法: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 FoundContent-Type: text/html; charset=utf-8Location: http://www.jb51.netServer: Microsoft-IIS/7.0Date: Fri, 13 Aug 2010 21:18:34 GMTContent-Length: 144<html><head><title>Object moved</title></head><body><h2>Object moved to <a href=http://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頁面跳轉的方法,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.