zoukankan      html  css  js  c++  java
  • ASP.NET页面跳转的三种方法比较

    在ASP.NET下,经常需要在页面之间跳转,下面我们来分别介绍一下关于.NET中Response.Redirect(),Sever.Execute(),Server.Transfer() 三种页面跳转的方法。

    一、Response.Redirect()

    优点:可以跳转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪)。

    缺点:

    (1)、跳转的速度慢。因为它要走2个来回(2次postback),首先是发送一个 http请求到客户端,通知需要跳转到新页面,然后客户端再发送跳转请求到服务器端。

    (2)、不能跳过登录保护

    (3)、跳转后前一个页面的内容将全部清空,如A页面跳转到B页面,则A页面的数据信息将会全部丢失。所以需要用Session,Cookies,Application等对象存储页面的数据。

    实例
    Example that uses Redirect [C#; ASP.NET]

     1 using System;
     2 using System.Web.UI;
     3 namespace WebApplication1
     4 {
     5   public partial class List : Page
     6   {
     7     protected void Page_Load(object sender, EventArgs e)
     8     {
     9       // 实例化一个response
    10       var response = base.Response;
    11       // Redirect temporarily.
    12       // Don't throw an HttpException to terminate.
    13       response.Redirect("http://www.jb51.net", false);
    14     }
    15   }
    16 } 

    Result of the page:

    1 HTTP/1.1 302 Found
    2 Content-Type: text/html; charset=utf-8
    3 Location: http://www.jb51.net
    4 Server: Microsoft-IIS/7.0
    5 Date: Fri, 13 Aug 2010 21:18:34 GMT
    6 Content-Length: 144
    7 <html><head><title>Object moved</title></head><body>
    8 <h2>Object moved to <a href="http://www.dotnetperls.com/list">here</a>.</h2>
    9 </body></html> 

    二、sever.execute

    这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。
    举个例子看看:
    1、创建一个web form
    2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
    3、为button按钮创建click事件
    代码如下:

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

    4、创建过程来返回TextBox1,TextBox2控件的值代码如下:

     1 public string Name
     2 {
     3   get
     4   {
     5     return TextBox1.Text;
     6   }
     7 }
     8 public string EMail
     9 {
    10   get
    11   {
    12     return TextBox2.Text;
    13   }
    14 } 

    5、新建一个目标页面命名为webform2
    6、在webform2中放置两个Label1,Label2
    在webform2的Page_Load中添加如下代码:

     1 private void Page_Load(object sender, System.EventArgs e)
     2 {
     3   //创建原始窗体的实例
     4   WebForm1 wf1;
     5   //获得实例化的句柄
     6   wf1=(WebForm1)Context.Handler;
     7   Label1.Text=wf1.Name;
     8   Label2.Text=wf1.EMail;
     9 } 

    三、server.transfer

    速度快,只需要一次postback ,但是。。。。他必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。你可以写个小程序试试:设计一个由页面一到页面二的跳 转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     3 <html xmlns="http://www.w3.org/1999/xhtml">
     4 <head runat="server">
     5 <title></title>
     6 </head>
     7 <body>
     8 <form id="form1" runat="server">
     9 <div>
    10 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    11 <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    12 </div>
    13 </form>
    14 </body>
    15 </html>
    16 .net代码
    17 using System;
    18 using System.Collections.Generic;
    19 using System.Linq;
    20 using System.Web;
    21 using System.Web.UI;
    22 using System.Web.UI.WebControls;
    23 public partial class WebForm1 : System.Web.UI.Page
    24 {
    25 public string Time
    26 {
    27 get { return DateTime.Now.ToString(); }
    28 }
    29 public string TestFun()
    30 {
    31 return "Function of WebForm1 Called";
    32 }
    33 protected void Page_Load(object sender, EventArgs e)
    34 {
    35 Context.Items.Add("Context", "Context from Form1");
    36 }
    37 protected void Button1_Click(object sender, EventArgs e)
    38 {
    39 //this.TextBox2.Text =Request ["TextBox1"].ToString ();
    40 Server.Transfer("WebForm2.aspx", true);//第二个参数为false时,WebForm2.aspx中不能获得TextBox1的内容
    41 }
    42 } 

    总结:

    如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。
    ·如果要确保 HTML输出合法,请使用Response.Redirect,因为Server.Execute 或者Server.Transfer方法返回给客户端的页面包含多个<Html><body>标记,不是合法的HTML页面,在 非IE浏览器中可能会发生错误。

  • 相关阅读:
    移动端适配
    javscript_DOM 拖动事件
    Nginx
    文件和二进制数据的操作
    数组方法
    JS异步开发
    网络协议
    八大排序
    硬链接与软链接的区别
    入门计算机一段时间的感想
  • 原文地址:https://www.cnblogs.com/xiesong/p/3672131.html
Copyright © 2011-2022 走看看