1
<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?

user2314737
  • 27,088
  • 20
  • 102
  • 114
Dirk
  • 23
  • 2
  • 8

2 Answers2

3

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>
Rex M
  • 142,167
  • 33
  • 283
  • 313
  • I just wondering where should I put this function is just before my function or on the header. sorry, I am new to javascript, can someone give me more details, thanks – Dirk Oct 26 '09 at 17:20
  • and I have been put this function in both header and in my updatepanel2, but it still doesn't work, want more details. – Dirk Oct 26 '09 at 17:22
  • 1
    @Dirk put the whole thing at the bottom of the page, after `

    `

    – Rex M Oct 26 '09 at 17:42
  • @Rex M, I have been put this code just after

    , which cause my label1 doesn't update any more, I think it's because I have the code which is used for updatepanel1

    – Dirk Oct 26 '09 at 18:43
  • 1
    @Dirk your `add_endRequest(functionName());` is an error. You are calling alert, which returns nothing. You need to pass a *reference* to the function you want to call - not call it. Putting `()` at the end invokes it right then and there. – Rex M Oct 26 '09 at 18:51
  • this is my code, the one before is just for testing use this is used to auto update the updatepanel1 and There is a condition to update updatepanel2 – Dirk Oct 26 '09 at 19:17
0

I have made it works by refreshing the whole page

Dirk
  • 23
  • 2
  • 8