Cross-Page Submission in ASP. NET 2.0

Source: Internet
Author: User
Tags html code example
ASP. NET 2.0 provides a simple cross-Page Submission method.

Simple Example

First, let's take a look at the followingCode, The button in firstpage. aspx can be submitted to the specified page by specifying the postbackurl attribute:

Firstpage. aspx

<% @ 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 username {
Get {
Return this.txt name. text;
}
}
Protected void button#click (Object sender, eventargs E)
{
Label1.text = "PostBack from self. Your name is:" + txtname. text;
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Meta content = "text/JScript" http-equiv = "content-script-type"/>
<Title> first page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<H3> the frist page Your name:
<Asp: textbox id = "txtname" runat = "server"/>
<Asp: Label id = "label1" runat = "server" enableviewstate = "false"/> <br/>
<Br/>
<Asp: button id = "button1" runat = "server" text = "PostBack to same page" onclick = "button#click"/> <br/>
<Br/>
<Asp: button id = "button2" runat = "server" text = "PostBack to second page" postbackurl = "~ /Secondpage. aspx "/> <br/>
</Div>
</Form>
</Body>
</Html>

Secondpage. aspx

<% @ page Language = "C #" %>
<% @ previouspagetype virtualpath = "~ /Firstpage. aspx "%>



second page




This is the second page


& nbsp;





Open firstpage. aspx, enter the content, and press the "PostBack to second page" button to submit the content. The page is submitted to secondpage. aspx, and the input content is displayed on secondpage. aspx. Note the postbackurl attribute added by button2 in firstpage. aspx, And the @ previouspagetype command in secondpage. aspx. These new content is part of the cross-Page Submission solution in ASP. NET 2.0.

You may find that when using the cross-Page Submission function of ASP. NET 2.0, the target page is opened in the window of the Source Page. However, sometimes we need to open the target page in a new window, which can be achieved by modifying the <form> attribute in the Source Page. The following code is used:

<Form ID = "mainform" target = "_ blank" runat = "server">

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 applicationProgramThe 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.

After the postbackurl attribute of the button control is set, the ASP. NET runtime binds a new JavaScript code to the HTML element corresponding to the button control. Use the new webform_dopostbackwithoptions function to replace the common previous _ dopostback function. The specific HTML code example is as follows:

<Input type = "Submit" name = "button2" value = "PostBack to second page" onclick = "javascript: webform_dopostbackwithoptions (New webform_postbackoptions (" button2 "," ", false, "", "secondpage. aspx ", false, false)" id = "button2"/>

In the preceding code, the JavaScript code of the webform_dopostbackwitexceptions function and webform_postbackoptions function is as follows:

Function webform_postbackoptions (eventtarget, eventargument, validation, validationgroup, actionurl, trackfocus, clientsubmit ){
This. eventtarget = eventtarget;
This. eventargument = eventargument;
This. Validation = validation;
This. validationgroup = validationgroup;
This. actionurl = actionurl;
This. trackfocus = trackfocus;
This. clientsubmit = clientsubmit;
}

Function webform_dopostbackwitexceptions (options ){
VaR validationresult = true;
If (options. validation ){
If (typeof (page_clientvalidate) = 'function '){
Validationresult = page_clientvalidate (options. validationgroup );
}
}
If (validationresult ){
If (typeof (options. actionurl )! = "Undefined") & (options. actionurl! = NULL) & (options. actionurl. length> 0 )){
Theform. Action = options. actionurl;
}
If (options. trackfocus ){
VaR lastfocus = theform. elements ["_ lastfocus"];
If (typeof (lastfocus )! = "Undefined") & (lastfocus! = NULL )){
If (typeof (document. activeelement) = "undefined "){
Lastfocus. value = options. eventtarget;
}
Else {
VaR active = Document. activeelement;
If (typeof (active )! = "Undefined") & (active! = NULL )){
If (typeof (active. ID )! = "Undefined") & (active. ID! = NULL) & (active. Id. length> 0 )){
Lastfocus. value = active. ID;
}
Else if (typeof (active. Name )! = "Undefined "){
Lastfocus. value = active. Name;
}
}
}
}
}
}
If (options. clientsubmit ){
_ Dopostback (options. eventtarget, options. eventargument );
}
}

When you click the button, the current form submits the content to the target page specified by the postpageurl attribute. When the page contains a control that can implement cross-page submission, the page creates a hidden field named _ previouspage, which contains information about the source page. The target page uses this information to create a complete reference to call the Source Page Object. The following is an example of the HTML code for the hidden fields:

<Input type = "hidden" name = "_ previouspage" id = "_ previouspage" value = "ND3_1GqjDSUeAC3yLYVz-eQrkTzZLYFHliIFf7mMQVBdmwZmFi8HG4mzX5pfZY0n0"/>

Summary

The cross-page submission feature added by ASP. NET 2.0 makes our development process more flexible. When cross-page submission is used, we need to select an appropriate method based on the actual situation to read the information on the Source Page. If the source page is in the same application as the target page, you can use the @ previouspagetype command to specify the Source Page, so that you can use the advantages of strong type reference.

Because ASP. the child controls contained in each page class in. Net correspond to protected members. Therefore, you cannot directly reference the controls on the access Source Page through previouspage, first, the attribute to be accessed on the source page must be disclosed. We recommend that you only disclose the required information as a public attribute to reduce the information that may be used by Potential Malicious users.

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.