问题
I have some fairly simple code here, but I can't for the life of me figure out why it's not working.
I have a page called Update.aspx which contains the following HTML:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Non Panel <%= Date.Now.ToLongTimeString%>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lbl" runat="server">Updates in 5</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
The code behind looks like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim t As New Timers.Timer
t.Interval = 5000
AddHandler t.Elapsed, AddressOf raiseupdate
t.Start()
End Sub
Private Sub raiseupdate(ByVal sender As Object, ByVal e As System.EventArgs)
sender.stop()
lbl.Text = Date.Now.ToLongTimeString
UpdatePanel1.Update()
End Sub
This is what I'm expecting to happen: The page displays the words "Updates in 5" within the update panel. The timer elapses, calls the raiseupdate() method, and the update panel update() method is called which refreshes the content of the update panel.
What actually happens is this: The timer elapses, the update panel update() method line is reached, but the data never seems to make it back to the page. That is, the words "Updates in 5" are never replaced with the current time.
So I figure I've run into some kind of fundamental misunderstanding about what the update() method actually does, but I can't figure out where I've gone wrong. What can I do to make this work?
回答1:
Looks like the two Updates.. One from the Update Panel and One from Timer is messing your code up.
Instead you can use ajax Timer and Add this as an AsyncPostBack triggers.. This should do the work for you..
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Non Panel <%= Date.Now.ToLongTimeString%>
</div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="5000" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lbl" runat="server">Updates in 5</asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
</asp:UpdatePanel>
Your VB code will look like this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
lbl.Text = Date.Now.ToLongTimeString
End Sub
If this does not work you can manually call the Update() in the Timer_tick event
来源:https://stackoverflow.com/questions/12565765/updatepanel-update-method-doesnt-update-the-contents-of-the-updatepanel-contr