Page value passing skills in ASP. NET

Source: Internet
Author: User

In ASP. there are several methods to pass values between pages in. Net: querystring. this method is the easiest, but its disadvantage is that it displays the value to be transferred in the address bar. If it is not a good solution for security information. Another drawback is that it cannot upload objects. This method is suitable for transmitting a simple value and less important information for security. Example:
There are two pages: webform1.aspx and webform2.aspx.
Place the following code in some events of webform1.aspx. CS:
String url = "webform2.aspx? Name = "incluthis.txt name. Text; // note that there is no space between name and value; otherwise, an error indicating no reference object will be reported.
Response. Redirect (URL );
Next, the critical moment appears: place the following code in some events in webform2.aspx. CS:
Lblname. Text = request. querystring ["name"];
OK! The entire value transfer process is completed!

Another method is to use session variables to pass values, which is also common. The use of session is flexible, you can
Upload values between multiple pages. After you call remove, the session becomes invalid. Take two pages as an example:
Write the following code in webform1.aspx. CS:
Session ["name"] = txtname. text;
Response. Redirect ("webform2.aspx ");
The following figure shows the session value in webform1.aspx. CS:
Lblname. Text = session ["name"]. tostring (); // The conversion type is required because the obtained object is
Session. Remove ("name ");? // Invalidate the session.

The third method is to use the request object for value (note: the HTML control is used here. The implementation is as follows:

In webform1.aspx:

Name:

?

Write response. Write (request. Form ["txtname"]) to an event in webform2.aspx. CS.

After you click the submit button, the value is retrieved through request. Form ["txtname,

The fourth method is to use the transfer () method of the server object to transmit values. It receives a page object.

The following code explains this implementation method:

Write the following code in an event of webform1.aspx: Server. Transfer ("webform2.aspx"); // pass the Page Object
Then, you can obtain the Page Object of webform1 in webform2.aspx. CS.
If (context. handler is webform1) // determines whether the passed object is webform1, because multiple passing objects may exist.
{
Webform1 F1 = (webform1) Context. Handler; // create a webform1 Page Object using context. handler and return an object
Response. Write ("hey, I get it with context handler" + (textbox) f1.findcontrol ("textbox1"). Text +"

");??
}
// Call the fincontrol () method of the webform1 object to find a text control named "textbox1" in webform1. You can also replace it with other controls, no matter which control you are looking for, you must forcibly convert it to its type. In this example, It is textbox. Finally, call its property text to obtain the value in textbox1, to achieve the effect of passing values.
This method is also flexible. You can pass multiple page objects instead of passing values, therefore, when you obtain a page object, you can obtain the values of some controls or other data in this page object.

(Note: In addition to request. Form (), the value passing method uses HTML controls, and other methods use Web controls)

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.