Detailed description of the ASP.net in the page--practical tips for communication

Source: Internet
Author: User

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

Using querysting to pass values between pages is a very old mechanism, the main advantage of this method is very simple to implement, but its disadvantage is that the value passed is displayed in the browser's address bar (unsafe), but also can not pass the object, 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 that return the form
    3. Create a character variable to save a URL in the Click event of a button or link button
    4. Add the querystring parameter to the saved URL
    5. Using the Response.Redirect redirect to the stored URL below demonstrates how to implement this method:

SOURCE page code:

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

Target page code:

private void Page_Load (object sender, System.EventArgs e) 
{ 
label1.text=request.querystring["name"]; 
label2.text=request.querystring["email"]; 
 

Using the session variable

Using 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 that 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. To redirect to another page using the Response.Redirect method
    5. Extract the value of the session on another page, and when you are sure that the session is not needed, explicitly clear the following code fragment demonstrates how to implement this method:

SOURCE page code:

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

Target page code:

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"); 

Using Server.Transfer

This method is slightly more complicated than the one described above. However, it is particularly 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, you need to write some extra code to create properties so that you can access it on another page. However, the benefits of this approach are also 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. To create a Get property procedure for a return value
    3. Create buttons and link buttons that return the form
    4. Call the Server.Transfer method in a button-click event handler 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
public string Name 
{get 
{TextBox1.Text} 
} 
public string EMail 
{ 
TextBox2.Text} 
}

Then call the Server.Transfer method

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

Target page code:

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; 
} 

Through this article on the transfer of parameters between the several methods of introduction, I hope to help you, but also hope that a lot of support cloud habitat community.

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.