Describes how to pass parameters between pages in ASP. Net. asp.net

Source: Internet
Author: User

Describes how to pass parameters between pages in ASP. Net. asp.net

ASP. NET provides an excellent event-driven programming model, allowing developers to simplify the overall design of the application, but this also causes some inherent problems, such as the use of traditional ASP, by using the POST method, we can easily transfer values between pages. Similarly, when using the event-driven programming model ASP. NET is not that easy. Of course, we still have some methods to implement the same function.

This article will try to solve this problem using different possible methods, but it is foreseeable that this article will include the querystring, session variable, and server. Transfer Method for cross-page value Transfer.

QueryString

Using QuerySting to pass values between pages is already a very old mechanism. The main advantage of this method is that it is very simple to implement, however, its disadvantage is that the passed value is displayed on the address bar of the browser (Insecure), and the object cannot be passed, however, this method is a good solution when the number of transmitted values is small and the security requirements are not high.

The steps for using this method are as follows:

  1. Use a widget to create a web form)
  2. Create button and link button that can return form
  3. Create a character variable to save the URL in the button or link button click event
  4. Add the QueryString parameter to the saved URL.
  5. Use Response. Redirect to the saved URL. 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 Session Variables

Using the Session variable is another way to pass values between pages. In this example, we store the value in the control in the Session variable and then use it on another page, to transfer values between different pages. However, it should be noted that storing too much data in the Session variable will consume a lot of server resources, so you should be careful when using the session. Of course, we should also use some cleanup actions to remove unnecessary sessions to reduce unnecessary resource consumption.

The general steps for transferring values using Session variables are as follows:

  1. Add necessary controls to the page
  2. Create button and link button that can return form
  3. In the Click Event of a button or link button, add the control value to the session variable.
  4. Redirect to another page using the Response. Redirect Method
  5. Extract the session value from another page. When you determine that you do not need to use this session, you need to explicitly clear the following code snippet to demonstrate 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 a little more complex than the method described above, but it is particularly useful in inter-page value transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious. In general, this method is concise and object-oriented.

The entire process of using this method is as follows:

  1. Add necessary controls to the page
  2. Create the Get attribute process of the returned value
  3. Create button and link button that can return form
  4. Click the button in the event handler to call the Server. Transfer Method to Transfer to the specified page.
  5. On the second page, we can use Context. the Handler property is used to obtain the reference of the previous page instance object. With it, you can use the value of the control to access the previous page. The following code comprehensively implements the code of the above step process: 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; } 

Through the introduction of several methods for passing parameters between pages, I hope to help you, and also hope to support more help.

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.