Summary of the jump function between C # pages _c# Tutorial

Source: Internet
Author: User
Tags dotnet

Introduction ASP. NET provides an excellent event-driven programming model that allows developers to simplify the overall design of the application, but this also creates some of its inherent problems, for example, using the traditional ASP, we can easily deliver values between pages by using the Post method, the same thing, The asp.net of the event-driven programming model is not so easy, of course, we still have some ways to do the same. This article will try to solve this problem using different possible methods, but it is expected that this article will contain the use of querystring,session variables and the server. Transfer method to implement the value transfer between pages.

Using querystring to pass values between pages is a very old mechanism, and the main advantage of this approach is that it is very simple to implement, but its disadvantage is that the value passed is displayed on the browser's address bar (unsafe), while not passing objects, However, this approach is a good solution when the value is low and the security requirements are not high. The steps to use this method are as follows:

1, create a Web Form with a control (form)

2, create buttons and link buttons to return the form

3, create a character variable to save the URL in the Click event of a button or link button

4, add the QueryString parameter in the saved URL

5, use Response.Redirect to redirect to the URL saved above

Below

Copy Code code as follows:

<strong>private void Button1_Click (object sender, System.EventArgs e)
{
string URL;
Url= "Anotherwebform.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
Response.Redirect (URL);
}
</STRONG>

Target page code:

Copy Code code as follows:

<strong>private void Page_Load (object sender, System.EventArgs e)
{
label1.text=request.querystring["Name"];
label2.text=request.querystring["email"];
}
</STRONG>

Code snippet demonstrates how to implement this method: the source page code:

Using the session variable to use the session variable is another way to pass a value between pages, in this case we have the value in the control in the session variable, and then use it in another page to implement the value transfer between different pages. However, it should be noted that in the session variable storage too much data will consume more server resources, in the use of sessions should be cautious, of course, we should also use some clean-up action to remove some unnecessary session to reduce the unnecessary consumption of resources. The general steps for passing values using session variables are as follows:

1, add the necessary controls to the page

2, create buttons and link buttons to return the form

3, in the Click event of a button or link button, add the value of the control to the session variable

4, redirect to another page using the Response.Redirect method

5, the value of the session is extracted on another page, and when it is determined that the session is not needed, the following code fragment is explicitly cleared to demonstrate how to implement this method:

SOURCE page code:

Copy Code code as follows:

<strong>private void Button1_Click (object sender, System.EventArgs e)
{
TextBox1 and TextBox2 are WebForm
Controls
session["Name"]=textbox1.text;
session["Email"]=textbox2.text;
Server.Transfer ("anotherwebform.aspx");
}
</STRONG>

Target page code:

Copy Code code as follows:

<strong>private void Page_Load (object sender, System.EventArgs e)
{
label1.text=session["Name"]. ToString ();
label2.text=session["Email"]. ToString ();
Session.remove ("name");
Session.remove ("email");
}
</STRONG>

Server.Transfer This method is a bit more complicated than the one described above, but it is especially useful in the transfer of values between pages, and you can use this method to access the exposed values in the same way as object attributes, and of course, using this method, You need to write some extra code to create properties so that you can access it on another page, but the benefits of this approach are obvious. Overall, the use of this approach is both concise and object-oriented. The whole process of using this method is as follows:

1, add the necessary controls to the page

2, create a Get property procedure that returns a value

3, create buttons and link buttons to return the form

4, call the Server.Transfer method in the button click event handler to transfer to the specified page

5, in the second page, we can use the Context.Handler property to get a reference to the previous page instance object, through which you can use the value of the control that accesses the previous page. The following code synthesizes the code to implement the procedure above:

SOURCE page code: Add the following code to the page

Copy Code code as follows:

<strong>public string Name {get {return textbox1.text;}}

public string EMail {TextBox2.Text}}
</STRONG>

Then call the Server.Transfer method

Copy Code code as follows:

<strong>private void Button1_Click (object sender, System.EventArgs e) {Server.Transfer ("anotherwebform.aspx");
</STRONG>

Target page code:

In anotherwebform.aspx, be sure to add in the first sentence

Copy Code code as follows:

<%@ Reference page= "~/gcsetting.aspx"%>

Then add the following in the Anotherwebform.aspx.cs.

Copy Code code as follows:

<strong>private void Page_Load (object sender, System.EventArgs e)
{
Create instance of source Web Form
WebForm1 WF1;
Get reference to current handler instance
wf1= (WebForm1) Context.Handler;
Label1.text=wf1. Name;
Label2.text=wf1. EMail;
}
</STRONG>

Add:

Response.Redirect Implementation page Jump

Function: Redirect current client browser to connect to another URL page.

Syntax: Response.Redirect ("string")

Description: The string is a Web page URL, can be an absolute or a relative path.

You can also swap its overloaded method Response.Redirect ("string", false), or, if set to True, the server will send the results of the page execution to the client when the page code is fully executed or the flush or End method is invoked, otherwise the server side executes the side. ASP3.0 the default value in the IIS5 above version defaults to true;windows2000 is also true.

Response.Redirect this jump page method jump speed is not fast, because it has to walk two times postback (postback). It can jump to any page, no Site page restrictions (can be jumped from Baidu to Google), but can not skip login protection. Slow speed is the biggest flaw.

Its jump mechanism is: first, server-side send an HTTP request to the client, notify the need to jump to the new page, and then the client sends a 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.

2.response.redirect two ways to open a new window

In general, the Response.Redirect method is to turn on the server side, so in addition to using Response.Write ("<script>window.location= ' http://dotnet.aspx.cc" ;</script> ") method, other methods are temporarily unable to open the specified URL address in a new window.

However, you can open a new window by setting the target property of the form element. Here are two ways you can use it.

Method One: Set the target property on the server side, which is also very useful for situations where the client does not support scripting. The code is as follows:

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<script runat= "Server" >

protected void Page_Load (object sender, EventArgs e)
{
Form1. Target = "_blank";
}

protected void Button1_Click (object sender, EventArgs e)
{
Response.Redirect ("http://dotnet.aspx.cc");
}
</script>

<title></title>
<body id= "B" runat= "Server" >
<form id= "Form1" runat= "Server" >
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "open a new window or new tab"/>
</form>
</body>

Option two: Set the target attribute using client-side scripting. The code is as follows:

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<script runat= "Server" >

protected void Page_Load (object sender, EventArgs e)
{
BUTTON1.ATTRIBUTES.ADD ("onclick", "this.form.target= ' _newname '");
}

protected void Button1_Click (object sender, EventArgs e)
{
Response.Redirect ("http://dotnet.aspx.cc");
}
</script>

<title></title>
<body id= "B" runat= "Server" >
<form id= "Form1" runat= "Server" >
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "open a new window or new tab"/>
</form>
</body>

The target attribute in the above two methods can take any legal name, but be aware that if a window with the same name is already open, the new window opens in a window that already has a name.

Update: If you need to set the width and height of the pop-up window, you can modify it to the following method:

Copy Code code as follows:

<strong><%@ Page language= "C #" autoeventwireup= "true"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<script runat= "Server" >

protected void Page_Load (object sender, EventArgs e)
{
String windowname = "Win" + System.DateTime.Now.Ticks.ToString ();
Page.registeronsubmitstatement ("JS", "window.open (', '" + Windowname + "', ' width=600,height=200 ')");
Form1. Target = Windowname;
}

protected void Button1_Click (object sender, EventArgs e)
{
Response.Redirect ("http://dotnet.aspx.cc");
}
</script>

<title></title>
<body id= "B" runat= "Server" >
<form id= "Form1" runat= "Server" >
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "open a new window or new tab"/>
</form>
</body>
</STRONG>

3. Server.Transfer speed, only need one postback, but it must be in the same site jump.
Server.Transfer's redirection request takes place on the server side, simply passing the control to a new Web page and not relocating the page at the client, so the browser's URL address is still the original page's address! In addition, it can skip login protection.
By default, the transfer method does not pass a form data or query string from one page to another, but if you set the second argument of the method to true, you can preserve the form data and query string for the original page.
Server.Transfer ("hyj.aspx");
The server stops executing this page, saves the current data on this page, then shifts the page to hyj.aspx and returns the results of the data before the jump and hyj.aspx page execution back to the browser.

4.server.execute This method is mainly used in page design, it must be in the same site to jump, and only need one postback. This method is used when inserting the output of one page into another ASPX page, mostly in a table where one page resembles a nested one in another. Server.Execute ("hyj.aspx");//The server saves the current data on this page, moves the page to hyj.aspx execution, returns to this page after hyj.aspx execution, and then merges the three results back to the browser. The Execute method jump is similar to a function call. How to select a page redirection method: asp.net page jumps----Redirect, Transfer, Execute, hyperlink, Hyperlink control. When you need to jump the user to a page on another server, use redirect when you need to jump the user to a non-ASPX page, such as HTML, using the redirect to pass the query string as part of the URL to the server, using redirect, Since the other two methods cannot do 2 postback, bringing the data back to the server requires a transformation between ASPX pages (not involving logins), using Execute when you need to insert the output of an ASPX page into another ASPX page. Transfer Use hyperlinks when you need a user to decide when to jump the page. You want to use programs to control the target of the transformation, but the timing of the conversion is determined by the user, using the Web server's hyperlink control to dynamically set its NavigateUrl property.

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.