【问题标题】:javascript popup in Timer control and UpdatePanel control not workingTimer 控件和 UpdatePanel 控件中的 javascript 弹出窗口不起作用
【发布时间】:2013-12-27 22:56:20
【问题描述】:

MasterPage.master

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" Interval="10000">
    </asp:Timer>
  </ContentTemplate>
</asp:UpdatePanel>



MasterPage.master.vb

Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    'some sql select codes

    con.Open()
    Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32) 'ExecuteScalar() only return 1 row and ignore rest
    con.Close()

    If result > 0 Then

        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "AlertMessageBox", "alert('hello world');", True)
        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "AlertMessageBox", "alert('" & result & " hello world');", True)
        ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), "AlertMessageBox", "alert(‘hello world’);", True)


    End If

End Sub



基本上我有一个Timer 控件,它以固定的时间间隔运行SQL Select,如果result 大于0,则会弹出警报。

如果我不使用UpdatePanel 但没有UpdatePanel,则代码可以正常工作,每当Timer 控件运行时,页面都会刷新,从而导致用户正在处理的内容丢失。



编辑:进一步澄清

Timer1_Tick 每 10 秒运行一次。问题出在弹窗上,浏览器没有弹窗。

【问题讨论】:

  • 不要使用更新面板。使用 jquery ajax 和 javascript settimout 页面不会刷新页面...

标签: javascript asp.net vb.net timer updatepanel


【解决方案1】:

OnTick="Timer1_Tick" 添加到您的.master 标记中。

另外,你应该不需要,但你可以尝试添加:

<Triggers>
  <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>

【讨论】:

  • 试过改成&lt;asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick"&gt;还是不行。
  • @j_t_fusion 我在答案中添加了更多内容。您可以尝试添加触发器吗?
  • 不行,还是不行。在我的原始代码中,Timer1_Tick 确实每 10 秒运行一次,问题是浏览器上没有弹出窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多