溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net中實現頁面跳轉的方式有哪些

發布時間:2021-02-20 17:02:46 來源:億速云 閱讀:276 作者:Leah 欄目:開發技術

這篇文章給大家介紹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>
<h3>Object moved to <a href=//www.jb51.net/list/index_1.htm>here</a>.</h3>
</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中實現頁面跳轉的方式有哪些就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女