Detail the method of transferring parameters between pages in ASP. Net

  • 2021-08-16 23:43:03
  • OfStack

ASP. NET provides an excellent event-driven programming model, Allows developers to simplify the overall design of applications, However, this also causes some inherent problems. For example, with the traditional ASP, we can easily pass values between pages by using POST method. The same thing is not so easy with ASP. NET with event-driven programming model. Of course, we still have 1 way to achieve the same function.

This article will try to solve this problem using different possible methods, but it can be expected that this article will include using querystring, session variables and server. Transfer methods to implement value transfer between pages.

Using QueryString

Using QuerySting to pass values between pages is an old mechanism, The main advantage of this method is that it is very simple to implement, but its disadvantage is that the passed value will be displayed on the address bar of the browser (insecure), and the object cannot be passed at the same time. However, this method is still a good scheme when the passed value is small and the security requirement is not high.

The steps to use this method are as follows:

Creating an web form with controls (form) Create buttons and link buttons that can return to the form Create a character variable to hold URL in the click event of button or link button Add QueryString parameters to saved URL Using Response. Redirect to redirect to URL saved above, the following code snippet 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"]; 
} 

Use the Session variable

Using Session variable is another way to pass values between pages. In this example, we store the values in the control in Session variable, and then use it in another page to achieve the purpose of passing values between different pages. However, it should be noted that storing too much data in Session variables will consume more server resources, so we should be careful when using session. Of course, we should also use some cleaning actions to remove some unnecessary session to reduce unnecessary consumption of resources.

The first steps to pass a value using an Session variable are as follows:

Add the necessary controls to the page Create buttons and link buttons that can return to the form Add the value of the control to the session variable in the click event of the button or link button Redirect to another page using the Response. Redirect method Extract the value of session on another page, and clear it explicitly when you determine that you don't need to use the session. The following code snippet demonstrates how to implement this method:

Source page code:


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

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

Use Server. Transfer

This method is slightly more complicated than the method described above. However, it is particularly useful in value transfer between pages. Using this method, you can access the exposed value in the form of object attributes on another page. Of course, using this method, you need to write an extra code to create 1 attribute so that you can access it on another page, but the benefits of this method are obvious. Generally speaking, using this method is concise and object-oriented.

The whole process of using this method is as follows:

Add the necessary controls to the page Procedure for creating an Get property for a return value Create buttons and link buttons that can return to the form Call the Server. Transfer method in the button click event handler to move to the specified page In the second page, we can use the Context. Handler property to get a reference to the previous page instance object, through which we can use the value of the control accessing the previous page. The following code synthesizes the code for the above steps: Source page code: Add the following code to the page

public string Name 
{ 
get   { return TextBox1.Text; } 
} 
public string EMail 
{ 
get { return 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; 
} 

Related articles: