欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

三種asp.net頁面跳轉(zhuǎn)的方法

 更新時間:2020年08月20日 14:40:20   投稿:lijiao  
跳轉(zhuǎn)頁面是大部編輯語言中都會有的,下面我們來分別介紹一下關(guān)于.net中response.redirect sever.execute server.transfer三種頁面跳轉(zhuǎn)的方法,,需要的朋友可以參考下

本文實例為大家分享了asp.net頁面跳轉(zhuǎn)的三種方法,供大家參考,具體內(nèi)容如下

第一種方法:response.redirect

這個跳轉(zhuǎn)頁面的方法跳轉(zhuǎn)的速度不快,因為它要走2個來回(2次postback),但它可以跳轉(zhuǎn)到任何頁面,沒有站點頁面限制(即可以由雅虎跳到新浪),同時不能跳過登錄保護。但速度慢是其最大缺陷!redirect跳轉(zhuǎn)機制:首先是發(fā)送一個http請求到客戶端,通知需要跳轉(zhuǎn)到新頁面,然后客戶端在發(fā)送跳轉(zhuǎn)請求到服務(wù)器端。需要注意的是跳轉(zhuǎn)后內(nèi)部空間保存的所有數(shù)據(jù)信息將會丟失,所以需要用到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.dbjr.com.cn", false);
 }
 }
}

代碼如下 

HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: //www.dbjr.com.cn

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

</html>

第二種方法sever.execute

這個方法主要是用在頁面設(shè)計上面,而且他必須是跳轉(zhuǎn)同一站點下的頁面。這個方法是需要將一個頁面的輸出結(jié)果插入到另一個aspx頁面的時候使用,大部分是在表格中,將某一個頁面類似于嵌套的方式存在于另一頁面。

舉個例子看看:

1、創(chuàng)建一個web form
2、在新建的web form中放置一個button1,在放置兩個TextBox1,TextBox2
3、為button按鈕創(chuàng)建click事件

代碼如下

private void Button1_Click
(object sender, System.EventArgs e)
{
Server.Transfer("webform2.aspx");
}

4、創(chuàng)建過程來返回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)
{
//創(chuàng)建原始窗體的實例
WebForm1 wf1;
//獲得實例化的句柄
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}

第三種方法:server.transfer

速度快,只需要一次postback ,但是它必須是在同一個站點下,因為它是server的一個方法。另外,他能跳過登錄保護。你可以寫個小程序試試:設(shè)計一個由頁面一到頁面二的跳轉(zhuǎn),但要進入到頁面二需要登錄,form認證,但如果跳轉(zhuǎn)語句使用transfer的話,那就不會彈出登錄頁面了。這個方法的重定向請求是發(fā)生在服務(wù)器端,所以瀏覽器的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);//第二個參數(shù)為false時,WebForm2.aspx中不能獲得TextBox1的內(nèi)容
  
 }
}

如果要捕獲一個ASPX頁面的輸出結(jié)果,然后將結(jié)果插入另一個ASPX頁面的特定位置,則使用Server.Execute。
·如果要確保HTML輸出合法,請使用Response.Redirect,因為Server.Execute 或者Server.Transfer方法返回給客戶端的頁面包含多個<Html><body>標記,不是合法的HTML頁面,在非IE瀏覽器中可能會發(fā)生錯誤。

以上就三種asp.net頁面跳轉(zhuǎn)的方法,希望對大家的學習有所幫助。

相關(guān)文章

最新評論