"Turn" to solve the conflict between UpdatePanel and jquery

Source: Internet
Author: User

More and more friends now like to use jquery with ASP. Recently, many Indians in the forum complained that UpdatePanel and jquery are incompatible, many jquery effects are not work after the UpdatePanel update. This article solves this compatibility problem by analyzing the framework structure of both.

Problem Recurrence:
1. Add ScriptManager and UpdatePanel to the page

2. Adding element A in the UpdatePanel

3. Add x effect to element a with jquery

4. Add a button B to the UpdatePanel as the postback

Result: On the first page load, the x effect of element A is normal, after clicking B, the page is refreshed locally, and element a loses the X effect.

Analysis 1:updatepanel
UpdatePanel is mainly used for local refresh in the application, avoiding the postback of the whole page.

The core of UpdatePanel implementation of local refresh is the Microsoftajaxwebform.js file, its local refresh process is to submit the page to the server (including viewstate), execute the service-side code asynchronously will be in the UpdatePanel HTML to re-render.

During this process, the rest of the page does not have a status change.

Analysis 2:jquery
jquery can add various attributes and event handles to HTML elements through simple code, where we can see the official documentation:

Tutorials:how JQuery Works
Http://docs.jquery.com/How_jQuery_Works

Here, we can tell that jquery has an important event labeled "Ready", generally the effects of HTML elements and event handles are added through this ready event, as follows:

$ (document). Ready (function () {

$ ("P"). Text ("The DOM is now loaded and can be manipulated.");
});
The official note to this is that the ready event will run once after the DOM is fully loaded, OK, so far, the cause of the problem is almost clear:

Reason:
Because element a is overridden in the UpdatePanel after a local refresh, and the entire DOM tree is not reloaded, jquery's ready event is not triggered, so element a loses its original effect.

Solution:
We can extract the code executed in the Ready event, and then execute the jquery initialization code once each UpdatePanel local flush, by capturing the ScriptManager endrequest event, as follows:

<script type= "Text/javascript" >
function Load () {
Sys.WebForms.PageRequestManager.getInstance (). Add_endrequest (Endrequesthandler);
}

function Endrequesthandler () {
ReadyFunction1 ();
ReadyFunction2 ();
ReadyFunction3 ();
}
</script>

<body onload= "Load ()" >

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.