UpdatePanel can be used to create a rich local update Web application, a very important control in the ASP.net 2.0 AJAX extensions, which is powerful in that no client script is written, You can automatically implement local updates as long as you add several UpdatePanel controls and a ScriptManager control to one page. Learn some other ways to use UpdatePanel (second article).
A To control the update of UpdatePanel with programming method
For UpdatePanel, we can also use programmatic methods to control its updates, and you can register an asynchronous committed control through the ScriptManager RegisterAsyncPostBackControl () method. and call the UpdatePanel Update () method to get it updated. Again with a boring time update example I used in the previous article to look at, sometimes I feel that the example is too complex to understand what to say, as shown in the following code, note that Button1 is not included in the UpdatePanel:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<script runat= "Server" >
void Button1_Click (object sender, EventArgs e)
{
This. Label2.Text = DateTime.Now.ToString ();
}
</script>
<title>refreshing an UpdatePanel programmatically</title>
<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server"/>
<div>
<asp:updatepanel id= "UpdatePanel1" runat= "Server" updatemode= "Conditional" >
<ContentTemplate>
<asp:label id= "Label1" runat= "Server" text= "Update Time:" ></asp:Label>
<asp:label id= "Label2" runat= "Server" text= "Label" forecolor= "Red" ></asp:Label><br/><br/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:button id= "Button1" runat= "Server" text= "button" OnClick = "Button1_Click"/>
</div>
</form>
</body>
This time needless to say, must be full page submission, run as shown in the following figure:
To modify the above example again, use the ScriptManager RegisterAsyncPostBackControl () registration Button1 for an asynchronous submit control, and invoke the UpdatePanel Update () method:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<script runat= "Server" >
void Page_Load (object sender, EventArgs e)
{
Scriptmanager1.registerasyncpostbackcontrol (Button1);
}
void Button1_Click (object sender, EventArgs e)
{
This. Label2.Text = DateTime.Now.ToString ();
This. Updatepanel1.update ();
}
</script>
<title>refreshing an UpdatePanel programmatically</title>
<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server"/>
<div>
<asp:updatepanel id= "UpdatePanel1" runat= "Server" updatemode= "Conditional" >
<ContentTemplate>
<asp:label id= "Label1" runat= "Server" text= "Update Time:" ></asp:Label>
<asp:label id= "Label2" runat= "Server" text= "Label" forecolor= "Red" ></asp:Label><br/><br/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:button id= "Button1" runat= "Server" text= "button" OnClick = "Button1_Click"/>
</div>
</form>
</body>
As you can see, you are already committing asynchronously: