Three methods of asp. net page jump

  • 2021-07-06 10:46:55
  • OfStack

In this article, we share three methods of asp. net page jump for your reference. The specific contents are as follows

Method 1: response. redirect

This method of jumping pages is not fast, because it has to go back and forth 2 times (postback 2 times), but it can jump to any page, there is no site page limit (that is, it can jump from Yahoo to Sina), and it can't skip login protection at the same time. But slow speed is its biggest defect! redirect jump mechanism: First, send an http request to the client, notify the need to jump to a new page, and then the client sends a jump request to the server. It should be noted that all data information stored in the internal space will be lost after jumping, so session is needed.

The code is as follows


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.ofstack.com", false);
 }
 }
}

The code is as follows


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

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

</html>

Method 2 sever. execute

This method is mainly used in the page design above, and he must be jumping to the same site under the page. This method is used when the output result of one page needs to be inserted into another aspx page, most of which is in the table, and a certain one page exists in another one page in a nested way.

Take an example:

1. Create an web form
2. Place one button1 in the newly built web form, and place two TextBox1 and TextBox2
3. Create an click event for the button button

The code is as follows


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

4. Create a procedure to return the value code for the TextBox1, TextBox2 control as follows:

The code is as follows


public string Name
{
get
{
 return TextBox1.Text;
}
}
public string EMail
{
get
{
 return TextBox2.Text;
}
}

5. Create a new target page named webform2
6. Place two Label1 and Label2 in webform2

Add the following code to Page_Load of webform2:

The code is as follows


private void Page_Load
(object sender, System.EventArgs e)
{
// Create an instance of the original form 
WebForm1 wf1;
// Get the instantiated handle 
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}

Method 3: server. transfer

Fast, only need postback once, but it must be in the same site, because it is a method of server. In addition, he can skip login protection. You can write a small program to try: design a jump from Page 1 to Page 2, but to enter Page 2, you need to log in and form authentication, but if the jump statement uses transfer, the login page will not pop up. The redirect request for this method occurs on the server side, so the browser's url address remains the address of the original page!

The code is as follows


<%@ 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 Code 
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);// No. 1 2 Parameters are false When, WebForm2.aspx Can't be obtained in TextBox1 Content of 
  
 }
}

Use Server. Execute if you want to capture the output of one ASPX page and then insert the results into a specific location on another ASPX page.
· If you want to ensure that the HTML output is legal, use Response. Redirect, because the Server. Execute or Server. Transfer methods return pages to the client containing multiple < Html > < body > Tag, is not a legitimate HTML page, an error may occur in a non-IE browser.

The above three methods of asp. net page jump, hoping to help everyone's study.


Related articles: