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.