Javascript – How to recall a javascript function after updatepanel update

cjavascriptupdatepanel

<asp:UpdatePanel ID="UpdatePanel2" runat="server"
    OnLoad="UpdatePanel2_Load" UpdateMode="Conditional">
    <ContentTemplate>
        <script type="text/javascript">
            myFunction();
        </script>
        <asp:Label ID="Label1" runat="server" Text="1" Width="18px"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

C#:

if(condition)
{
      UpdatePanel2.Update();
}
protected void UpdatePanel2_Load(object sender, EventArgs e)
{
        Label1.Text += 1;
}

the Label1's value is changing but it doesn't call myFunction(). I want to call it one more time after the some condition but not using setTimeout to auto call, some ideas?

Best Answer

When the UpdatePanel updates the DOM and rewrites script blocks into the DOM, they are not re-executed. You need to get on the client-side event that fires after the UpdatePanel is finished and re-execute your JS blocks:

<script type="text/javascript">
    function handleEndRequest(sender, args)
    {
        var panel = document.getElementById(sender._postBackSettings.panelID);
        var scripts = panel.getElementsByTagName('script');
        for(var i=0;i<scripts.length;i++) {
            eval(scripts[i].innerHTML);
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
</script>