[ASP. NET Ajax] How to register JavaScript Functions after updatepanel updated

Source: Internet
Author: User

As an ASP. NET fellow, updatepanel just like one artifacts. It can make all the contents in one updatepanel get an partial-Refresh (Ajax) feature.
Usually, we need to call a JavaScript function after we update the content in the updatepanel. without updatepanel, We cocould add onclick attribute to one button or use page. clientscript to register the script in code-behind. but in the updatepanel, page. clientscript will lose efficacy.
In this document, I want to discuss some methods to register JavaScript Functions after the updatepanel updated.
ASP. NET extension provide us some useful and direct ways to call a JavaScript function both in client-side and server-side.
Client-side:
Method:
Look at this code snippet:

01 <Form Id = "Form1" Runat = "Server" >
02 <Asp: scriptmanager Id = "Scriptmanager1" Runat = "Server" >
03 </ASP: scriptmanager>
04
05 <Script Type = "Text/JavaScript" >
06 Function Pageload ( Sender , E ) {
07 Alert ( "Page is re-loaded! " );
08 }
09 </SCRIPT>
10
11 <Asp: updatepanel Id = "Updatepanel1" Runat = "Server" >
12 <Contenttemplate>
13 < % = Datetime. Now. tostring () %>
14 </Contenttemplate>
15 <Triggers>
16 <Asp: asyncpostbacktrigger Controlid = "Triggertimer" Eventname = "Tick" />
17 </Triggers>
18 </ASP: updatepanel>
19
20 <Asp: Timer Id = "Triggertimer" Runat = "Server" Interval = "5000" >
21 </ASP: timer>
22 </Form>

In this snippet, We cocould see we registerPageloadFunction directly in the page. The function pageload will be executed when the page first load and each asynchronous PostBack which caused by updatepanel.

Method B:

The code above in pageload is same as the code below.

 

1 <Script Type = "Text/JavaScript" >
2 Window . Onload = Function () {
3 Sys . Application . Add_load ( Function () {
4 Alert ( "Page re-loaded! " );
5 });
6 }
7 </SCRIPT>

 

Server side:

For server side, we need to use a method, scriptmanager. registerclientscriptblock, for more details about this method, please view this link in msdn:Http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerclientscriptblock.aspx. This method registers a client script block with the scriptmanager Control for use with a control that is inside an updatepanel control, and then adds the script block to the page.

For example,

 

1 Protected Void Triggertimer_tick ( Object Sender , Eventargs E )
2 {
3 Scriptmanager . Registerclientscriptblock ( This , This . GetType (), "Regscript" ,
4 "Alert ('page re-loaded! ');" , True );
5 }

The result is same as the methods which talked above.

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.