asp.net Ajax Primer Series (5): Using UpdatePanel controls (ii)

Source: Internet
Author: User
Tags datetime tostring xmlns

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:

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.