How do i run a javascript event after a postback within an updatepanel
            Asked
            
        
        
            Active
            
        
            Viewed 1e+01k times
        
    5 Answers
79
            
            
        You can use endRequest event of PageRequestManager.
<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>   
            <asp:Button runat="server" Text="Button" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>
<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function (s, e) {
        alert('Postback!');
    });
</script>
        m3kh
        
- 7,881
 - 2
 - 31
 - 37
 
- 
                    I've tried the above method but get a Script Error saying Sys is undefined.... any ideas? – Mych Feb 20 '13 at 15:06
 - 
                    1@Mych The "Sys is undefined" error occurs when you place the prm before the "asp:ScriptManager". In order for this to work, the javascript code for the prm variable declaration and the add_endRequest must be after the script manager. Furthermore, VB.net users may want to remind themselves that the "Sys.WebForms..." is case-sensitive unlike the code-behind it resembles. – Ross Brasseaux Jun 27 '13 at 13:47
 - 
                    This works great but blocking further postbacks. any solution? – Vijay Kumbhoje Mar 07 '16 at 05:55
 
27
            You can use the ClientScriptManager to make a call to a function on the reload:
ClientScriptManager.RegisterStartupScript(this.GetType(), "AKey", "MyFunction();", true);
        Paddy
        
- 33,309
 - 15
 - 79
 - 114
 
- 
                    
 - 
                    I do it like this $("#divGetListforUser .btn").click(function() { isClicked = true; }); and set it false after the postback – Kimtho6 Nov 22 '10 at 09:53
 - 
                    i used `ClientScript.RegisterOnSubmitStatement()` to reload captcha within a `!IsPostBack` if block and it works like a charm, solved my issue because captcha was a user control etc – Niklas Feb 24 '20 at 05:23
 
13
            
            
        Simply
<script type="text/javascript"> 
    function pageLoad() { 
  } 
</script>
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server"> 
  <ContentTemplate> 
    <asp:Button runat="server" ID="Button1" /> 
    <asp:Literal runat="server" ID="TextBox1" /> 
 </ContentTemplate> 
</asp:UpdatePanel>
pageLoad() will then continue to be called each time Button1 is triggered
        Mina Gabriel
        
- 23,150
 - 26
 - 96
 - 124
 
5
            
            
        Try this:
$.ajax({
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......
});
        M.Mohammadi
        
- 1,558
 - 1
 - 13
 - 25
 
- 
                    1Thanks, that helped me alot. It's clean and no Backend stuff is needed. – Megajin Jul 06 '16 at 07:43
 - 
                    best! you could add a _dopostback in the before and exec something after, fanstastic! – elle0087 Jul 15 '20 at 09:00
 
1
            
            
        For use within an UpdatePanel i would use the ScriptManager.RegisterStartupScript
        Tim B James
        
- 20,084
 - 4
 - 73
 - 103