asp.net (C #) page jump Seven Ways to summarize _ practical skills

Source: Internet
Author: User
①response.redirect
This jump page method jump speed is not fast, because it will take 2 back (2 times postback), but he can jump to any page, no Site page restrictions (that is, can be jumped from Yahoo to Sina), while not skip login protection. But slow speed is its biggest flaw!redirect jump mechanism: First is sends an HTTP request to the client, the notification needs to jump to the new page, then the client sends the jump request to the server side. Note that all the data information stored in the internal space after the jump is lost, so you need to use the session.
Instance
Example that uses Redirect [C #; asp.net]
Copy Code code 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 Code code as follows:

http/1.1 302 Found
content-type:text/html; Charset=utf-8
Location:http://www.jb51.net
server:microsoft-iis/7.0
Date:fri Aug 21:18:34 GMT
content-length:144
</body>

②sever.execute
This method is mainly used on the page design, and he must be to jump the same site under the page. This method is needed to insert the output of one page into another ASPX page, mostly in a table where one page is similar to nesting in another.
For example, take a look at:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
The code is as follows:
Copy Code code as follows:

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

4, the creation process to return the value of the Textbox1,textbox2 control code is as follows:
Copy Code code as follows:

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

5, a new target page named WebForm2
6. Place two Label1,label2 in WebForm2
Add the following code to the Page_Load of WebForm2:
Copy Code code 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;
}

③server.transfer
Fast, only need one postback, but .... He must be under the same site as it is a method of the server. In addition, he can skip login protection. You can write a small program to try: Design a page to page two of the jump, but to enter the page two need to login, form certification, but if the jump statement using transfer words, it will not pop-up login page. The redirection request of this method occurs on the server side, so the URL address of the browser still retains the address of the original page!
Copy Code code 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 ">
<title></title>
<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>
. 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), or false with the second argument, TextBox1 content is not available in webform2.aspx
}
}

Summary:
Use Server.Execute if you want to capture the output of an ASPX page and then insert the result into a specific location in another ASPX page.
• If you want to make sure that the Html output is legitimate, use Response.Redirect because the Server.Execute or Server.Transfer method returned to the client page contains multiple <Html><body> tags , it is not a legitimate HTML page, and errors may occur in non-IE browsers.

1.response.redirect ("Http://www.jb51.net", false);
The target page and the original page can be on 2 servers, and you can enter a URL or relative path. The bool value that follows is whether to stop the current page from executing.
Jumps to the new page, the original window is replaced. "
The URL in the browser is a new path.
: The Response.Redirect method causes the browser to link to a specified URL. When the Response.Redirect () method is invoked, it creates an answer that is indicated in the reply header.
Status Code 302 (indicates that the target has changed) and a new target URL. The browser receives the response from the server and uses the information in the answer header to issue a request to the new URL. Say
When using the Response.Redirect method, the redirection occurs on the client and involves two communications with the server (two back and forth): the first is a request to the original page,
Gets a 302 response, the second is a new page that is declared in the Request 302 response, and the page after the redirect.
2.server.transfer ("Default2.aspx?name=zhangsan", true);
The target page and the original page can be on the same server.
Jumps to the new page, the original window is replaced.
The URL in the Wave Ball Forum browser is the same as the original path.
By default, the Server.Transfer method does not pass the form data or query string from one page to another, but simply set the second parameter of the method to
Tb310true, you can keep the form data and query string for the first page.
At the same time, the use of Server.Transfer should be noted that the target page will use the original page created by the response stream, which led to ASP.net machine verification check b310,, Bo Ball, Bo Net, Connoisseur Heart water, tournament recommendations, race analysis, database, football tournament, basketball, NBA, odds, Score, basketball data, soccer data.

(Machine authentication Check,mac) that the viewstate of the new page has been tampered with. Therefore, if you want to preserve the form data and the collection of query strings for the original page,
The target Page page directive's enableViewStateMac property must be set 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, then jump to the original page of the transfer.
The URL in the browser is the same as the original path.
When the specified ASPX page completes, the control process returns to the location where the original page issued the Server.Execute call.
This kind of page navigation is similar to a function call to an ASPX page, where the called page has access to the form data and the collection of query strings that emit the calling page, so
The enableViewStateMac property of the called Page command is set to False.
4.response.write ("<script language= ' JavaScript ' >window.open (' aaa.aspx ');</script>"); _
The target page and the original page can be on 2 servers, and you can enter a URL or relative path.
The original window is reserved and a new page is added.
5.response.write ("<script language= ' javascript ' >window.location= ' default2.aspx ' </script>");
Opens a new page, the original window is replaced.
6.response.write ("<script>window.showmodaldialog (' default2.aspx ') </script>");
7.response.write ("<script>window.showmodelessdialog (' default2.aspx ') </script>");
(a), ShowModalDialog and showModelessDialog What is the difference?
ShowModalDialog: The input focus is always maintained when it is opened. The user cannot switch to the main window unless the dialog box is closed. Similar to the running effect of alert. B310,, Bo Ball, Bo Net, expert heart water, tournament recommendations, race analysis, database, football tournament, basketball, NBA, odds, scores, basketball data, football data
showModelessDialog: After being opened, the user can randomly switch the input focus. Has no effect on the main window (at most, it is blocked.) :P)
(b), how to let the ShowModalDialog and showmodelessdialog hyperlinks do not eject a new window?
 Add <base target= "_self" to the Web page you've opened. > This sentence is usually 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.