Click here to Skip to main content
Click here to Skip to main content
Go to top

ASP.NET – jQuery is not Working in UpdatePanels

, 28 Jan 2013
Rate this:
Please Sign up or sign in to vote.
When using JavaScript or jQuery in UpdatePanel enabled web page, everything will work fine at the first page load, but after any asynchronous post back happens by UpdatePanel, all JavaScript and jQuery effects will vanish.

UpdatePanel

Introduction

ASP.NET UpdatePanel control allows to build rich, client-centric Web applications. UpdatePanel controls refresh only selected part of the page instead of refreshing the whole page with a post back. In another words this is performing a partial-page update. A Web page that contains a ScriptManager control and one or more UpdatePanel controls can automatically participate in partial-page updates, without custom client script.

When using JavaScript or jQuery in UpdatePanel enabled web page, everything will work fine at the first page load, but after any asynchronous post back happens by UpdatePanel, all JavaScript and jQuery effects will vanish.

Why is this happening?

Because, When the UpdatePanel post back triggers, the existing markup will overwrite with the markup generated by UpdatePanel on the post back, which destroys all event handlers from HTML elements in the UpdatePanel.

What is the solutions?

There are a couple of ways to fix this. In this article, IMO the easiest solution for this problem is to use Sys.Application inside UpdatePanel to call JavaScript, jQuery functions after each Asynchronous post back event.

Example:

In example below, I used an asp button for post back event and jQuery to show a message when Click Me! link is clicked.

JavaScript/jQuery function:

<script type="text/javascript">
    // JavaScript funciton to call inside UpdatePanel
    function jScript() {
        $("#click").click(function () {
            alert("Clicked Me!");
        });
    }
</script>

ASP.NET UpdatePanel with JavaScript/jQuery re-call function:

<%-- Script Manager for ASP.NET AJAx --%>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <%-- // Script Manager End --%>
    <%--ASP.NET UpdatePanel control to refresh page withtout post back--%>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <%-- Contetn template to put all ASP.NET controls for AJAX --%>
        <ContentTemplate>
        <%-- Re-Calling JavaScirpt/jQuery function on each Asynchronous post back. --%>
            <script type="text/javascript" language="javascript">
                Sys.Application.add_load(jScript);
            </script>
            <%-- // Re-Calling function End --%>
            <%-- ASP.NET button for post back --%>
            <asp:Button ID="btnPostBack" runat="server" 
               OnClick="btnPostBack_Click" Text="Click To Postback" />
            <%-- // ASP.NET button End --%>
            <%-- Link for showing the message on its click event --%>
            <a href"#" id="click">Click Me!</a>
            <%-- // Link End --%>
        </ContentTemplate>
        <%-- // Contetn template End --%>
    </asp:UpdatePanel>
    <%-- // ASP.NET UpdatePanel End --%>

ASP.NET (C#) code for button:

protected void btnPostBack_Click(object sender, EventArgs e)
{
    btnPostBack.Text = "UpdatePanel PostBack Happened";
}

Conclusion

After calling JavaScript/jQuery functions inside UpdatePanel using Sys.Applicaiton.add_load() event handler, you will be able to solve the problem.

Feel free to comment, suggest or give your feedback.

The post jQuery $(document).ready is not Working in UpdatePanels – ASP.NET appeared first on Noor Ahmad Feroozi's Blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Noor Ahmad Feroozi
Software Developer (Senior) Ministry of Education
Afghanistan Afghanistan
Over eight years of IT industry experience, and have been a part of design and development projects for many exceptional companies during those years, including Kabul Cyber Park, Alpha Omega Services, Ministry of Defense, Spacetech ICT Solutions, Ministry of Education and many others.
 
Technologies I use are standards-based XHTML, CSS, JavaScript, jQuery, PHP, ASP.Net, C#.Net, MS SQL, MySQL and SQLite. Each project I develop is built to be search engine and user friendly, using the latest development principles and techniques. To find out more about what I can do for you, check out my work at http://recentsolutions.net, or get follow me on http://twitter/naferoozi
Follow on   Twitter   Google+

Comments and Discussions

 
Questiondetail Pinmemberthewazz29-Jan-13 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by Noor Ahmad Feroozi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid