Asp.net (c #) webpage jump seven Methods Summary

Source: Internet
Author: User
Tags microsoft iis

① 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]
Copy codeThe 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 ("http://www.jb51.net", false );
}
}
}

Result of the page
Copy codeThe Code is as follows:
HTTP/1.1 302 Found
Content-Type: text/html; charset = UTF-8
Location: http://www.jb51.net
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:
Copy codeThe Code is as follows:
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:
Copy codeThe 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 Label1 and Label2 in webform2.
Add the following code to Page_Load of webform2:
Copy codeThe Code is as follows:
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!
Copy codeThe 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 = "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.

1. Response. Redirect ("http://www.jb51.net", false );
The target page and the original page can be on two servers. You can enter the URL or relative path. The value of bool is whether to stop executing the current page.
Jump to the new page, the original window is replaced. "
The URL in the browser is the new path.
: Response. Redirect method causes the browser to link to a specified URL. When the Response. Redirect () method is called, it creates a Response, which indicates
Status Code 302 (indicating that the target has changed) and the new target URL. The browser receives the response from the server and sends a request to the new URL using the information in the response header. That is to say,
When the Response. Redirect method is used, the redirection operation occurs on the client, involving two communications with the server (two back and forth) in total: the first request to the original page,
Get a 302 response, and the second request is the new page stated in the 302 response, get the page after redirection.
2. Server. Transfer ("Default2.aspx? Name = zhangsan ", true );
The target page and the original page can be on the same server.
Jump to the new page, the original window is replaced.
The URL in the browser of the popball Forum remains the same as the original path.
By default, the Server. Transfer method does not pass form data or query strings from one page to another, but you only need to set the second parameter of this method
Tb310True, the form data and query string of the first page can be retained.
At the same time, use Server. note that the target page will use the response stream created on the original page, which leads to ASP. NET machine verification check b310, Bo Qiu, Bo qiu.com, experts, game recommendations, event analysis, database, football events, basketball, NBA, odds, score, basketball data, football data.

(Machine Authentication Check, MAC) The ViewState of the new page has been tampered. Therefore, to retain the form data and query string set of the original page,
You must set the EnableViewStateMac attribute of the target Page command to False.
3. Server. Execute ("Default5.aspx? Address = beijing );
The target page and the original page can be on the same server.
Jump to the new page, and then jump to the original page.
The URL in the browser remains the same as the original path.
When the specified ASPX page is executed, the control process returns to the location where the original page is called by Server. Execute.
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page.
Set the EnableViewStateMac attribute of the Page command to False.
4. Response. Write ("<script language = 'javascript '> window. open ('aaa. aspx'); </script> ");_
The target page and the original page can be on two servers. You can enter the URL or relative path.
The original window is retained, and a new page is added.
5. Response. Write ("<script language = 'javascript '> window. location = 'default2. aspx' </script> ");
Open a new page, and the original window is replaced.
6. Response. Write ("<script> window. showModalDialog ('default2. aspx ') </script> ");
7. Response. Write ("<script> window. showModelessDialog ('default2. aspx ') </script> ");
(1) What are the differences between showModalDialog and showModelessDialog?
ShowModalDialog: after being opened, the input focus is always maintained. You cannot switch to the main window unless the dialog box is closed. Similar to the running effect of alert. B310, Bo Qiu, Bo qiu.com, experts, event recommendation, event analysis, database, football events, basketball, NBA, odds, score, basketball data, football data
ShowModelessDialog: after opening, you can randomly switch the input focus. It has no effect on the main window (it can be blocked at most. : P)
(2) How can we avoid a new window in the hyperconnection between showModalDialog and showModelessDialog?
Add <base target = "_ self"> to the opened webpage. This sentence is generally placed between

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.