Cross-page PostBack collection

Source: Internet
Author: User

The cross-page PostBack mechanism added to ASP. NET 2.0 allows ASP. NET developers to send post requests across pages easily (without customization. But in actual development, it is inevitable to encounter some minor problems. For example, when a cross-Page Submission occurs when you click the button, you need to first pop up a javascript confirm dialog box to confirm. If the user is OK, PostBack occurs. If cancel is dropped, the user will stay on the original page without any operation, similar to the client function we added on the delete button. At this point, we can't help it ......

 

We prepare two pages. In default. in aspx, add the text box and button, and set the button to send to target across pages. aspx: <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "Cross page post back" postbackurl = "~ /Target. aspx "onclientclick =" Return confirm ('are U sure? ') "/> In target. aspx. CS, find the textbox in default. aspx and display the value on the label: protected void page_load (Object sender, eventargs E)
{
If (previouspage! = NULL)
Label1.text = (previouspage. findcontrol ("textbox1") as textbox). text;
} Run, and then we found that when we click Cancel, we can indeed drop the cancel, but click OK, but the request cannot be sent in any way. What makes cross-page requests unable to be submitted? Probably with the onclientclick = "Return confirm ('are U sure? ') ", So let's take a look at ASP in the browser. net source code generated to achieve the cross-page PostBack effect: <input name = "textbox1" type = "text" id = "textbox1"/>
<Input type = "Submit" name = "button1" value = "Cross page post back" onclick = "Return confirm ('are U sure? '); Webform_dopostbackwithoptions (New webform_postbackoptions (& quot; button1 & quot;, & quot;, false, & quot;, & quot; target. aspx & quot;, false, false) "id =" button1 "/>

 

In the past, cross-page delivery relied on calling a javascript webform_dopostbackwithoptions () function before submit, and the return confirm () We needed was added before the function call. Therefore, if the confirm () method returns false, return false without any action. If the user clicks the OK button and confirm () returns true, it is equivalent to onclick = "Return true ;", therefore, the webform_dopostbackwithoptions () function cannot be called.

 

After analyzing the cause, find a solution. In the displayed confirm dialog box, must return cofirm () be used? Since the return value of the confirm () method is of the bool type, can we put it in the IF statement? Can we change the call in onclick to the form of IF (confirm () {dosomething?

 

Let's rewrite the button: onclientclick = "If (confirm ('are U sure? ') "After the test, we found that cross-page PostBack occurred when we clicked cancel. <Input name = "textbox1" type = "text" id = "textbox1"/>
<Input type = "Submit" name = "button1" value = "Cross page post back" onclick = "If (confirm ('are U sure? '); Webform_dopostbackwithoptions (New webform_postbackoptions (& quot; button1 & quot;, & quot;, false, & quot;, & quot; target. aspx & quot;, false, false) "id =" button1 "/> the reason is ASP. net will automatically add a semicolon after our JavaScript statement, so we can write: onclientclick = "If (confirm ('are U sure? ') Return false; "the cross-page delivery can fully meet our expectation. The code generated in the browser is: <input name = "textbox1" type = "text" id = "textbox1"/>
<Input type = "Submit" name = "button1" value = "Cross page post back" onclick = "If (! Confirm ('are U sure? ') Return false; webform_dopostbackwithoptions (New webform_postbackoptions (& quot; button1 & quot;, & quot;, false, & quot;, & quot; target. aspx & quot;, false, false) "id =" button1 "/>

 

As mentioned above, we have solved some minor problems that may occur when using the cross-page PostBack mechanism. Although the problem is small, what attracts the attention of beginners is the solution to the problem. Where does programming experience come from? It actually comes from the idea of solving the problem, and the idea of solving the problem also comes from the flexible application of the knowledge we reserve. The knowledge points involved in the above solutions are probably reserved for beginners. The rest is that we need to actively find a solution through thinking and trying in the actual programming environment. Here is a vulgar saying: there is no problem that cannot be solved.

 

Next, can we study the working mechanism of cross-page PostBack in this way? This mechanism is not available in ASP. NET 1.1 and is only available in 2.0. What is the magic?

First, we only see the call to the webform_dopostbackwithoptions () function, so we need to find the definition of this function. By viewing the source code of the client, we did not find the definition of the function, but found a <SCRIPT> tag that references an external webresource. axd file: <SCRIPT src = "/crosspagepostback/webresource. axd? D = zovwxdkqx08x9p6zrcqvia2 & amp; T = 633694239333996679 "type =" text/JavaScript "> </SCRIPT> paste the virtual path in SRC to the address bar of the browser, press enter, the browser prompts you to download the webresource. axd file. After downloading the file, open it with a text document. The first function is the webform_postbackoptions () parameter, and the second function is webform_dopostbackwithoptions. We can see that the 5th parameter actionurl of webform_postbackoptions () is called with & quot; target. aspx & quot;, that is, "target. aspx ", double quotation marks are encoded. What does webform_dopostbackwithoptions do for actionurl? If (typeof (options. actionurl )! = "Undefined") & (options. actionurl! = NULL) & (options. actionurl. length> 0 )){
Theform. Action = options. actionurl;
}

 

In fact, the actionurl is set to the form (that is, <form name = "form1" method = "Post" Action = "default. aspx "id =" form1 ">, in default. in the client code of aspx, you can find var theform = document. forms ['form1. This is clear! ASP. by default, the form data of each page is submitted to the page itself (Action = "default. aspx "is automatically generated, and it is useless to modify itself), called PostBack. Cross-page PostBack does not allow submitting post requests to other actions! In essence, JavaScript is used to modify the action attribute of a form element before submit (the _ dopostback () function is called at the end of the function.

 

Is there a lot of knowledge about the cross-page PostBack mechanism? Following this idea, we can often analyze ASP by ourselves. what are the Net Framework prepared for us, how they work, how I want them to be used for me, how I want to expand and improve them so that they can work better for me.

 

Enjoy!

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.