Javascript update problem. ASP.net UpdatePanel
I have some Javascript that I'm writing out to a literal control inside an UpdatePanel. I've set it to alert the current time every 15 seconds but I always get the original time. When I use Firebug I can see that the Script is getting updated. Is this because its a partial page refresh? How can I stop this happening. I've added the OutputCache directive to try and stop caching to see if that was the problem but that hasn't made any difference.
Here's code sample. Front End:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updTest" runat="server" UpdateMode="Always">
<ContentTemplate>
<script type="text/javascript">
function initScript(){
<asp:literal id="litUpdateScript" runat="server" />
}
</script>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger EventName="Tick" ControlID="panelRefresh" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="panelRefresh" Interval="15000" runat="server"></开发者_C百科asp:Timer>
Code Behind:
Protected Sub panelRefresh_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles panelRefresh.Tick
Me.litUpdateScript.Text = "alert('" & Now.ToString() & "');"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.litUpdateScript.Text = "alert('" & Now.ToString() & "');"
Dim script As String = "initScript();"
ScriptManager.RegisterStartupScript(updTest, updTest.GetType(), "Myscript", script, True)
End Sub
This Causes the Correct time on page load but doesn't change on timer refreshes. Although I can see the script changing in firebug.
I have a guess, I admittedly didn't actually try this but have worked with javascript and update panels quite a bit.
I think you should try registering the event in the tick instead of the page_load. Something more like this:
Protected Sub panelRefresh_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles panelRefresh.Tick
Me.litUpdateScript.Text = "alert('" & Now.ToString() & "');"
Dim script As String = "initScript();"
ScriptManager.RegisterStartupScript(this.updatePanel, typeof(string), "alertScript", "alert('Error Message');", true);
End Sub
The looking around on Google I did said that people had issues with page_loads but got them to work in other events. Worth a shot, let me know.
精彩评论