In ASP. ET 1. x, many friends may need to handle cross-page submission, that is, from page A can be submitted to page B, and even different control targets have different processing pages. Developers, especially those transferred from ASP, JSP, and PHP, may have such requirements. However, unfortunately, in ASP. NET 1.x, processing such cross-page requests is very ugly and requires a lot of "skillful" processing.
In ASP. NET 2.0, cross-Page Submission has a very reasonable solution. The following is an example.
Sourcepage. aspx: Pay attention to the postbackurl attribute setting of button1
<% @ Page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
Public String yourname
{
Get
{
Return this. textbox1.text;
}
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" text = "enter your name" width = "183px"> </ASP: Label>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "Submit" postbackurl = "~ /Targetpage. aspx "/> </div>
</Form>
</Body>
</Html>
Targetpage. aspx: Pay attention to the attribute settings of previouspagetype.
<-- <Br>
Code highlighting produced by actipro codehighlighter (freeware)
Http://www.CodeHighlighter.com/
-->
<% @ Page Language = "C #" %>
<% @ Previouspagetype virtualpath = "~ /Sourcepage. aspx "%>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
Protected void page_load (Object sender, eventargs E)
{
This. label1.text = previouspage. yourname;
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server"> </ASP: Label>
</Div>
</Form>
</Body>
</Html>
OK. With these two simple attribute settings, you can easily obtain cross-page submission features.
Read the Source Page Information
In ASP. NET 2.0, the button control has a new attribute postbackurl, which is used to set the target page to be submitted. As long as the postbackurl attribute of the button control is specified, it can be submitted to other pages. We can use multiple controls in the page to configure its postbackurl attribute and submit it to different pages. Of course, you can configure multiple pages to submit to the same page.
After cross-Page Submission, we usually need to read the control information (that is, the information sent by the browser) from the source page and the Public attributes of the Source Page.
Read Control Value
A previouspage attribute is added to the page class of ASP. NET 2.0. As the name suggests, this property on the target page contains references to the Source Page. In this way, the source page information can be accessed through the previouspage attribute on the target page. We generally use the findcontrol method to find the controls on the Source Page and read the values of these controls. The following code illustrates how to use this method:
If (page. previouspage! = NULL)
{
Textbox txtname = (textbox) page. previouspage. findcontrol ("txtname ");
If (txtname! = NULL)
{
Label1.text = txtname. text;
}
}
When we want to find that the control on the Source Page belongs to another control or template, we cannot directly use the findcontrol method to read it. Instead, we should first obtain the reference to this container, then you can find the control to be obtained in the container. In the following example, the firstpage. ASPX page contains a panel control whose ID is mainpanel and the Textbox Control whose ID is username. The Code is as follows:
Panel mainpanel = (panel) previouspage. findcontrol ("mainpanel ");
If (mainpanel! = NULL)
{
Textbox username = (textbox) mainpanel. findcontrol ("username ");
If (username! = NULL)
{
Label1.text = username. text;
}
}
Else
{
Label1.text = "cannot find username control .";
}
Read the public attributes of the Source Page
Once the previouspage reference is obtained on the target page, you can access the attributes of public controls on the Source Page, and also the public attributes on the Source Page. Of course, we need to disclose the properties to be accessed on the Source Page in advance to access the target page.
To obtain a Public Member of the Source Page, you must first obtain a strong type reference to the Source Page. In the first example, we can use the @ previouspagetype command to specify the source page, which has two attributes: virtualpath and typename. Use the virtualpath attribute to specify the virtual path (including the file name) of the Source Page, or use typename to specify the attribute of the Source Page. Note that only one of them can be specified, and both of them will be invalid. As shown in the first example:
<% @ Previouspagetype virtualpath = "~ /Firstpage. aspx "%>
If the @ previouspagetype command is used, the previouspage attribute on the target page is forcibly typed as the class on the Source Page. Therefore, you can directly reference public members on the Source Page. Another way to obtain a strong type reference to the Source Page is to include a @ reference command in the target page, just like referencing any other type of reference to be used in the page. In this case, you can obtain the previouspage attribute of the target page on the target page and forcibly convert it to the type of the Source Page, as shown in the following code:
Sourcepage_aspx sourcepage;
Sourcepage = (sourcepage_aspx) previouspage;
Label1.text = sourcepage. Username;
Read form information from the source page
If the source and target pages belong to the same ASP. NET application, the previouspage attribute on the target page contains references to the Source Page. If the @ previouspagetype command is not used, convert the previouspage attribute type to page on the target page.
Note: If the target page is not sent across pages or the target page is in different applications, the previouspage attribute is not initialized.
If the source page and target page belong to different applications or even different websites, you cannot directly obtain the control value on the Source Page, but you can obtain the control value from the request. form. Note that the view status of the Source Page cannot be read from the source page because it is hashed. If you want to store values on the Source Page and make these values available to target pages in other applications, you can store these values as strings in hidden fields on the Source Page, and use request. form to access them.
Determine whether cross-page submission is performed
When cross-page submission, the content of the Source Page Control is submitted to the target page, and then the Browser executes the post operation (note, not get ). In ASP. NET 1.x, because the pages are submitted to themselves, you can use the ispostback attribute of the page to determine whether the pages are submitted. However, when cross-page submission is performed, the ispostback attribute of the target page is false. To determine whether cross-page submission is required, you can determine the iscrosspagepostback attribute on the reference page returned by the previouspage attribute of the target page, as shown in the following code:
If (previouspage! = NULL)
{
If (previouspage. iscrosspagepostback = true)
{
Label1.text = "cross-Page Submission ";
}
}
Else
{
Label1.text = "non-Cross-Page Submission ";
}
Note: If the current page is not a target page submitted across pages, its previouspage attribute is blank.
Cross-Page Submission vs server. Transfer
In ASP. NET 2.0, you can use the previousoage attribute to obtain references to the Source Page, whether cross-Page Submission or server. transfer operations. If you want to differentiate them, you can use the method described above.
The following are some differences between cross-Page Submission and server. Transfer:
Genus
Cross-Page Submission
Server. Transfer
Ispostback
False
False
Previouspage
Source Page reference
Source Page reference
Previouspage. iscrosspagepostback
True
False
Iscrosspagepostback
False
False
Iscallback
False
False
Cross-page submission is the behavior of the client browser, while server. transfer is the behavior of the server. In the following section, we will analyze how the client browser submits cross-page submissions.
Simple Analysis of cross-Page Submission
In the above example, we mentioned setting the postbackurl attribute of the button to implement cross-Page Submission. In fact, only controls that implement the ibuttoncontrol interface can achieve this. Buttons, imagebutton, and linkbutton all implement the ibuttoncontrol interface. By implementing ibuttoncontrol, the custom control can also have the cross-Page Submission function of buttons in the form. The ibuttoncontrol interface aggregates some attributes of most button controls supported by ASP. NET 1.x (including some HTML button controls.
Read the Source Page Information
In ASP. NET 2.0, the button control has a new attribute postbackurl, which is used to set the target page to be submitted. As long as the postbackurl attribute of the button control is specified, it can be submitted to other pages. We can use multiple controls in the page to configure its postbackurl attribute and submit it to different pages. Of course, you can configure multiple pages to submit to the same page.
After cross-Page Submission, we usually need to read the control information (that is, the information sent by the browser) from the source page and the Public attributes of the Source Page.
Read Control Value
A previouspage attribute is added to the page class of ASP. NET 2.0. As the name suggests, this property on the target page contains references to the Source Page. In this way, the source page information can be accessed through the previouspage attribute on the target page. We generally use the findcontrol method to find the controls on the Source Page and read the values of these controls. The following code illustrates how to use this method:
If (page. previouspage! = NULL)
{
Textbox txtname = (textbox) page. previouspage. findcontrol ("txtname ");
If (txtname! = NULL)
{
Label1.text = txtname. text;
}
}
When we want to find that the control on the Source Page belongs to another control or template, we cannot directly use the findcontrol method to read it. Instead, we should first obtain the reference to this container, then you can find the control to be obtained in the container. In the following example, the firstpage. ASPX page contains a panel control whose ID is mainpanel and the Textbox Control whose ID is username. The Code is as follows:
Panel mainpanel = (panel) previouspage. findcontrol ("mainpanel ");
If (mainpanel! = NULL)
{
Textbox username = (textbox) mainpanel. findcontrol ("username ");
If (username! = NULL)
{
Label1.text = username. text;
}
}
Else
{
Label1.text = "cannot find username control .";
}
Read the public attributes of the Source Page
Once the previouspage reference is obtained on the target page, you can access the attributes of public controls on the Source Page, and also the public attributes on the Source Page. Of course, we need to disclose the properties to be accessed on the Source Page in advance to access the target page.
To obtain a Public Member of the Source Page, you must first obtain a strong type reference to the Source Page. In the first example, we can use the @ previouspagetype command to specify the source page, which has two attributes: virtualpath and typename. Use the virtualpath attribute to specify the virtual path (including the file name) of the Source Page, or use typename to specify the attribute of the Source Page. Note that only one of them can be specified, and both of them will be invalid. As shown in the first example:
<% @ Previouspagetype virtualpath = "~ /Firstpage. aspx "%>
If the @ previouspagetype command is used, the previouspage attribute on the target page is forcibly typed as the class on the Source Page. Therefore, you can directly reference public members on the Source Page. Another way to obtain a strong type reference to the Source Page is to include a @ reference command in the target page, just like referencing any other type of reference to be used in the page. In this case, you can obtain the previouspage attribute of the target page on the target page and forcibly convert it to the type of the Source Page, as shown in the following code:
Sourcepage_aspx sourcepage;
Sourcepage = (sourcepage_aspx) previouspage;
Label1.text = sourcepage. Username;
Read form information from the source page
If the source and target pages belong to the same ASP. NET application, the previouspage attribute on the target page contains references to the Source Page. If the @ previouspagetype command is not used, convert the previouspage attribute type to page on the target page.
Note: If the target page is not sent across pages or the target page is in different applications, the previouspage attribute is not initialized.
If the source page and target page belong to different applications or even different websites, you cannot directly obtain the control value on the Source Page, but you can obtain the control value from the request. form. Note that the view status of the Source Page cannot be read from the source page because it is hashed. If you want to store values on the Source Page and make these values available to target pages in other applications, you can store these values as strings in hidden fields on the Source Page, and use request. form to access them.
This article Reprinted from the network base camp: http://www.xrss.cn/Dev/DotNet/200752313471.Html