Button and page delivery

Source: Internet
Author: User
Cause:
Today, two updatepanels are used in the project to Implement Asynchronous page updates. Use a button in updatepanel to control content updates in another updatepanel. It is strange whether the content displayed is the original content. For the sake of simplicity, I will use a simple example to describe the problems in my project.
First page Code : < Body >
< Form ID = " Form1 " Runat = " Server " >
< ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server " >
</ ASP: scriptmanager >
< Div >
< ASP: updatepanel ID = " Updatepanel1 " Runat = " Server " >
< Contenttemplate >
< ASP: button ID = " Button1 " Runat = " Server " Text = " Button " Onclick = " Button#click "   />
< ASP: Label ID = " Label1 " Runat = " Server " Text = " Beforesubmit " > </ ASP: Label >
</ Contenttemplate >
</ ASP: updatepanel >
< ASP: updatepanel ID = " Updatepanel2 " Runat = " Server " Updatemode = " Always " >
< Contenttemplate >
< ASP: textbox ID = " Textbox2 " Runat = " Server " >
</ ASP: textbox >
< ASP: button ID = " Button2 " Runat = " Server " Text = " Button "   /> </ Contenttemplate >
</ ASP: updatepanel >
</ Div >
</ Form >
</ Body >

On the page, I defined two updatepanel and used the button1 in updatepanel to control the value of textbox2 in updatepanel2. The background code is as follows:

  Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Textbox2.text=Label1.text;
}

Protected   Void Button#click ( Object Sender, eventargs E)
{
Label1.text= "Aftersubmit";
}

}

Modify the value of textbox2 in the page_load () method, but the execution result is as follows:

The value in textbox2 does not change as you might think. It is still "beforesubmit". However, if you submit the page again, the value in textbox2 becomes "aftersubmit ".
Why? By setting breakpoints, it is found that when the button returns the page, the page_load () method is executed first, and then the button#click (Object sender, eventargs e) method in the onclick event is executed. This is the root cause of the problem.
It's really embarrassing to say that when so many button onclick events are used, we don't even know what work the system did when the event was triggered, leading to this error. Ah, it seems that I will learn more in the future.

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.