三种asp.net页面跳转的方法 |
本文标签:asp.net,页面跳转 第一种方法:response.redirect 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. // ... Dont 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: http://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=http://www.jb51.net/list/index_1.htm>here</a>.</h2> </body> </html> 第二种方法sever.execute 举个例子看看: 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 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 <%@ 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 。 以上就三种asp.net页面跳转的方法,希望对大家的学习有所帮助 。 |