Thursday, 6 November 2014

JQuery issue after partial postback

I came to know this theory after much R&D. I was not aware why JQuery is not get fired after partial postback. Then from google, I found solution as below. We have to register JQuery events after end of partial postback.

<asp:ScriptManager ID="smngr" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="upanel" runat="server">
            <ContentTemplate>
                <asp:Button ID="btn" Text="Test" runat="server" OnClick="btn_Click" />
                <input type="button" value="check" id="btncheck" />
            </ContentTemplate>
        </asp:UpdatePanel>

<script>

        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler)
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)

        function beginRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "disabled";
        }

        function endRequestHandler() {
            var btn = document.getElementById('<%= btn.ClientID %>');
            btn.disabled = "";
            register();
        }

        $(document).ready(function () {
            register();
        });

        function register()
        {
            $("#btncheck").click(function () {
                alert("Hello");
            });
        }


    </script>

1 comment: