views:

621

answers:

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

+1  A: 

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
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
and I have been put this function in both header and in my updatepanel2, but it still doesn't work, want more details.
Dirk
@Dirk put the whole thing at the bottom of the page, after `</body>`
Rex M
@Rex M, I have been put this code just after </body>, which cause my label1 doesn't update any more, I think it's because I have the code<script type="text/javascript">var prm = Sys.WebForms.PageRequestManager.getInstance(); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(alert("1")); setTimeout("Update()", 1000);function Update() { prm._doPostBack('UpdatePanel1', '');setTimeout("Update()", 1000); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(displayVideo());}</script> which is used for updatepanel1
Dirk
@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
this is my code, the one before is just for testing use <script type="text/javascript"> var prm = Sys.WebForms.PageRequestManager.getInstance(); setTimeout("Update()", 1000); function Update() { prm._doPostBack('UpdatePanel1', ''); setTimeout("Update()", 1000); }</script>this is used to auto update the updatepanel1 and There is a condition to update updatepanel2
Dirk
A: 

I have made it works by refreshing the whole page

Dirk
A: 

Accept the answer or post your solution as it will be beneficial to the community, thanks.

steven hong