Use server. Transfer to pass controls on the page

Source: Internet
Author: User

Special Method for transferring values between pages on server. Transfer

What you are familiar with when passing values between pages
(1) URL string value passing
(2) Session value transfer
(3) directly read the data on the page from server. Transfer.
The first two won't be mentioned. Everyone knows how to use them. There may be fewer people to use later. Here is an introduction.
A web form page is a class in an application, so you can create attributes for it just like any class. However, since Web form pages actually exist only during page execution, their lifecycles are very short. Therefore, attributes on web forms pages are of limited purpose because they only exist during page processing. However, if a specific technique is used to pass a control from one page to another, the latter page can still access the properties on the previous page.
For example, a label is placed on a webform page (page. aspx ),

Private void page_load (Object sender, system. eventargs E)
{
This. label1.text = "test ";
}

We set its value and set a public attribute for this page class.

Public label lab
{
Get
{
Return label1;
}
}

The returned result is a label on the page.
Then place a button on this page, and add the following code to the button's click event:

Server. Transfer ("Newpage. aspx ");

Place a label in Newpage. aspx to display the label value of the previous page, and add the following code in page_load:

If (! Ispostback)
{
Page P = (PAGE) context. Handler;
This. label1.text = "The passed value is:" + P. Lab. text;
}

 

We run this program and click the button to find that the value has been passed, but the browser URL shows the original page. aspx, indicating server. transfer does not change the URL of the browser. In fact, this indicates that the event is only completed on the server side, but not on the client side, this is why data on the previous page can be obtained in stateless HTTP. If you change to response. redirect, you cannot get anything. Analyze the above Code Newpage P = (Newpage) context. handler means to create an instance variable of the Source Page class, and then assign an HTTP object to it (an instance of the ihttphandler class), that is, the object that receives the initial request. The advantage of passing values in this way is that the memory is not used like the session, and it is especially suitable for big data (in fact, this cannot be called passing values). The disadvantage is that it can only be between pages on the same server, in addition, the URL is not changed, and users may be confused after reading it.

You can further improve the program. We now upload a label instance. You can transfer all objects between pages, or even directly use the DataGrid on the previous page!

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.