Technical Features published across web pages: 1. controls can be transmitted to the displayed new web page, 2. Only redirect to the target webpage where the original webpage is located in the same web application. 3. You can read the value and public attribute from the source webpage. 4. Do not use the information of the target webpage to update the browser. information, following the previous step in the browser will cause unexpected behavior usage: In crosspagesource. set the postbackurl attribute of the button in aspx to crosspagetarget. aspx in crosspagetarget. add the following program to the page_load event of aspx: protected void page_load (Object sender, eventargs e) {textbox txtusername = (textbox) previouspage. findcontrol ("txtusername"); txtmst. TEXT = "your input name is:" + txtusername. text;} cross-page publishing technology must be attached The postbackurl attribute of the button only supports the "button", "linkbutton", and "imagebutton" types. The public attribute values are obtained from the source webpage. 1. In crosspagetarget. set <% @ previouspagetype virtualpath = "~ /Crosspagesource. aspx "%> used: txtmsg. Text = previouspage. GetUserName; 2. Use <% @ reference virtualpath = "~ /Crosspagesource. aspx "%> when reading: @ reference has a strong type feature. The advantage is that you can directly access protected void page_load (Object sender, eventargs e) through intelliisense) {// convert previouspage to the same strong referencesource sourcepage = (referencesource) previouspage as the source page; txtmsg. TEXT = "the name you entered is:" + sourcepage. getUserName + "," + sourcepage. city;} The ispostback attribute iscrosspageepostback must be used to determine cross-page publishing. Therefore, the following program is available: protected void page_load (Object sender, eventargs E) {// Convert previouspage to the same strong if (previouspage! = NULL) // this check is required. You must use the cross-page publishing mechanism to ensure the integrity of the program {If (previouspage. iscrosspagepostback) {iscrosspagesource sourcepage = (iscrosspagesource) previouspage; txtmsg. TEXT = "the name you entered is:" + sourcepage. getUserName + "," + sourcepage. city ;}}principles of cross-page publishing: 1. Specify the target Webpage Through the postbackurl attribute of the button when the source webpage is published to the target webpage, after you press the button, the user directs to the target webpage. 2. After the target webpage stores the viewstate of the source webpage, the State stored in the viewstate of the source webpage is discarded, 3. When the previouspage object is used on the target webpage, the system will automatically initialize the same type of page (previewpage) as the source webpage ), in addition, in the page_complete stage of the target page, the viewstate stored in the original storage is restored to previouspage4, while previouspage indicates a new entity identical to the Source Page, and the original viewstate is injected, this is why the source page can be accessed at the target. If the viewstate is large, it indicates that the storage and restoration costs are high, therefore, cross-page publishing and querystring parameter passing are recommended.
Several methods for transferring data between pages
When transferring data between pages, we have the following options:
1. query string
A common method: query string is the part after the question mark in the URL. Its advantage is that it is lightweight and does not impose any burden on the server. It also has several disadvantages: The transmitted information is limited to simple strings and must be a valid URL character. The information is visible to the user, so there is a security problem; the user may try to manually modify the query string, which may be unexpected or cannot be prevented by the program; many browsers have restrictions on the URL Length (usually 1 kb to 2 kb ).
2. Cookie
Cookies are small files created on the client hard disk (or, if they are temporary, they are in memory. Its advantage is that it is not easy to be noticed by users, can be used by every page in the program, and can store data for a long time. But it also has some disadvantages that are the same as query string: It is limited to simple string information. Once the user finds the corresponding files, they are also easy to access and read. Therefore, we recommend that you do not use cookies to store complex or private information.
3. Session
You can save the data in the session on the Source Page and then read the data on the target page. Note: storing a large amount of information in a session seriously affects the server performance.
4. server. Transfer
To redirect the server, you can use server. Transfer. Because it is executed on the server side, the server. transfer method does not need to request another page. Httpcontext allows you to access data on the source page on the target page. The disadvantage is that the browser does not know that another page is returned to it. It will display the URL of the first page in the address bar, which will confuse users, they may also have trouble using bookmarks. Therefore, this method is not recommended.
5. Others
You can also use cache to store data and access the cache anywhere in the program. We recommend that you use cache only for data that is not frequently modified but frequently used. In addition, application variables, such as the number of clicks on the page, can be used in certain situations.
UseCross-page PostBack
ASP. NET 2.0 introduces a new method: Cross-page commit, that is, the PostBack is triggered on another page. This technology sounds simple, but there are hidden risks. Accidentally, the page you created will be tightly coupled and difficult to maintain and debug.
The cross-Page Submission mechanism is a property named postbackurl, which is defined by the ibuttoncontrol interface. The button controls that implement this interface include imagebutton, linkbutton, And button. Set the postbackurl property value to the name (URL) of another web form. When you click the button, the page is submitted to the new URL.
See the following example. The example includes crosspage1.aspx on the Source Page and crosspage2.aspx on the target page:
Crosspage1.aspx
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> source page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Firstname: <asp: textbox runat = "server" id = "txtfirstname"> </ASP: textbox>
<Br/>
Lastname: & nbsp; <asp: textbox runat = "server" id = "txtlastname"> </ASP: textbox>
<Br/>
<Asp: button runat = "server" id = "cmdsubmit" postbackurl = "~ /Crosspage2.aspx "text =" cross-page PostBack "/>
</Div>
</Form>
</Body>
</Html>
Crosspage1.aspx does not contain any code. The effect is as follows:
Click the button to submit the page to crosspage2.aspx. At this time, the crosspage2.aspx page can use the page. previouspage attribute to interact with crosspage1.aspx. The following event processing function demonstrates how to obtain the title of the Source Page and display it:
If (previouspage! = NULL)
{
Lblinfo. Text = "you came from a page titled" + previouspage. header. title;
}
Note: This method performs a null check on the previouspage object before accessing it. If the result is false, the cross-page commit is not performed. That is, crosspage2.aspx is directly requested, or, the previouspage object is unavailable.
Retrieve more data from the Source Page
The example above makes an interesting attempt, but we still cannot transmit any useful information.
To obtain the value of the control on the Source Page, use the findcontrol method:
If (previouspage! = NULL)
{
Lblinfo. Text = "you came from a page title" + previouspage. header. title;
String firstname = (previouspage. findcontrol ("txtfirstname") as textbox). text;
String lastname = (previouspage. findcontrol ("txtlastname") as textbox). text;
Lblinfo. Text + = "<br/> ";
Lblinfo. Text + = "your full name:" + firstname + "" + lastname;
}
To get more information, we need to convert the previouspage reference to the appropriate page class (in this example, the crosspage1 class ):
If (previouspage! = NULL)
{
Crosspage1 prevpage = previouspage as crosspage1;
If (prevpage! = NULL)
{
// You can access the public attributes of the Source Page.
}
}
In addition to type conversion in the code, you can also add the previouspagetype indicator on the. ASPX page:
<% @ Previouspagetype virtualpath = "~ /Crosspage1.aspx "%>
In this case, the crosspage1 type is automatically used for the previouspage attribute, and intelligent prompts in the editor can also be used. However, this method is quite fragile, because you can only use one page class! Therefore, for the sake of flexibility, it is better to use the type conversion method.
Well, no matter what, the previouspage object has been converted to a suitable page type, but you still cannot directly access the control objects it contains. This is because these controls are declared as the protection type (protected). The solution is to use properties.
For example, if you want to publish the values of two text box controls on the Source Page, you can add properties to encapsulate the control objects. For example, add properties to the crosspage1 class:
Public textbox firstnametextbox
{
Get {return txtfirstname ;}
}
Public textbox lastnametextbox
{
Get {return txtlastname ;}
}
However, this is usually not the best method. The problem is that it exposes too many details and the target page can read all the content of the text box control. If you need to modify the source page after a period of time and decide to use different input controls, it is quite difficult to maintain these attributes, because you have to modify the code of the two pages.
A better way is to define more specific attributes. They should only provide what you need. For example, you can consider adding a fullname attribute that reads the values of two text boxes:
Public String fullname
{
Get {return this.txt firstname. Text + "" + this.txt lastname. Text ;}
}
In this way, the relationship between the two pages becomes clear, simple, and easy to maintain. If you decide to use the new input control in crosspage1, you only need to modify the crosspage1 page. The code in crosspage2 is also modified as follows:
If (previouspage! = NULL)
{
Lblinfo. Text = "you came from a page titled" + previouspage. header. Title + "<br/> ";
Crosspage1 prevpage = previouspage as crosspage1;
If (prevpage! = NULL)
{
Lblinfo. Text + = "you typed in this:" + prevpage. fullname;
}
}
The following is the final result of crosspage2:
Cross-page submission is indeed very useful, but they also make the page complex. If you allow multiple source pages to be submitted to the same target page, you have to write code logic to determine where the page comes from and then process it accordingly. To avoid this annoyance, the simple method is to use it only between two specific pages.