Asp.net page Jump Methods

Source: Internet
Author: User
Tags microsoft iis

Jump pages are available in most editing languages. On the front, let's introduce the methods for redirecting from response. redirect sever.exe cute server. transfer to. net.

① Response. redirect
The method used to jump to the page is not fast because it takes two rounds (two postback), but it can jump to any page, there is no website page restriction (that is, you can jump from Yahoo to Sina), and you cannot skip logon protection. However, slow speed is the biggest defect! Redirect jump mechanism: First, send an http request to the client. The notification needs to jump to a new page, and then the client sends a jump request to the server. It should be noted that after the jump, all data information stored in the internal space will be lost, so session is required.

Instance

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

The Code is as follows: Copy code

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.bKjia. c0m", false );
}
}
}

Result of the page

The Code is as follows: Copy code

HTTP/1.1 302 Found
Content-Type: text/html; charset = UTF-8
Location: http://www.bKjia. c0m
Server: Microsoft-Microsoft IIS/7.0
Date: Fri, 13 Aug 2010 21:18:34 GMT
Content-Length: 144

<Html> <H2> Object moved to <a href = "http://www.dotnetperls.com/list"> here </a>. </Body>

Eclipsever.exe cute

This method is mainly used in the page design, and it must jump to the page under the same site. This method needs to be used to insert the output results of a page to another aspx page. Most of them are in the table, where a page is nested to another page.


For example:

1. Create a web form

2. Place a button1 in the new web form, and place two TextBox1 and TextBox2

3. Create a click event for the button

The Code is as follows:

The Code is as follows: Copy code

Private void button#click

(Object sender, System. EventArgs e)

{

Server. Transfer ("webform2.aspx ");

}

4. The TextBox1 is returned during the creation process. The value code of the TextBox2 control is as follows:

The Code is as follows: Copy code

Public string Name

{

Get

{

Return TextBox1.Text;

}

}

Public string EMail

{

Get

{

Return TextBox2.Text;

}

}

5. Create a new target page named webform2

6. Place Label1 and Label2 in webform2.

Add the following code to Page_Load of webform2:

The Code is as follows: Copy code

Private void Page_Load

(Object sender, System. EventArgs e)

{

// Create an instance of the original form

WebForm1 wf1;

// Obtain the instantiated handle

Wf1 = (WebForm1) Context. Handler;

Label1.Text = wf1.Name;

Label2.Text = wf1.EMail;

}

 

③ Server. transfer

Fast, only one postback, .... It must be on the same site because it is a server method. In addition, he can skip logon protection. You can try to write a small program: design a jump from page 1 to page 2, but to enter page 2, You need to log on and perform form authentication. However, if the jump statement uses transfer, the logon page will not pop up. The redirection request of this method occurs on the server, so the url address of the browser still retains the address of the original page!

The Code is as follows: Copy code

<% @ 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 = "button#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 button#click (object sender, EventArgs e)
{
// This. TextBox2.Text = Request ["TextBox1"]. ToString ();
Server. Transfer ("WebForm2.aspx", true); // when the second parameter is false, TextBox1 content cannot be obtained in WebForm2.aspx.

}
}

Summary:

If you want to capture the output results of An ASPX page and insert the results to a specific location of another ASPX page, use Server. Execute.

· If you want to ensure that the HTML output is valid, use Response. redirect, because Server. execute or Server. the Transfer method returns multiple <Html> <body> tags to the client's page, which is not a legal HTML page and may cause errors in non-ie browsers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.