System.Web.UI.Timer在启用时刷新页面

本文关键字:刷新 启用 Web UI Timer System | 更新日期: 2023-09-27 18:21:36

在page_load期间,我禁用了计时器。当我按下按钮1时,我启用了计时器,但页面会刷新。因此,它永远不会达到timer_tick1。我需要在点击一个按钮一定时间后显示一个弹出窗口。如何防止刷新发生?

警报等级

public static class Alert
{
    public static void Show(string message, Page page)
    {   
        // replaces the quotations to follow the script syntax
        // quotations are interpretated as ''' in script code
        string cleanMessage = message.Replace("'", "'''");
        string script = "<script type='"text/javascript'">alert('" + cleanMessage + "');</script>";
        // Gets the executing web page
        Page tempPage = page;
        // Checks if the handler is a Page and that the script isn't already on the page
        if (tempPage != null & !tempPage.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            tempPage.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script); // this isn't working, but it works on a button click event.
        }
    }
}

页面类

public partial class Test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostback) {
        Timer1.Enabled = false;
        Label2.Text = "Panel refreshed at: " +
          DateTime.Now.ToLongTimeString(); // Checks if page reloads
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {   // i added a breakpoint here. It doesn't even pass through. 
        Alert.Show("hehehehe", this); //PopUp Shows up. 
        Timer1.Enabled = false; //Cancels Timer
        Label1.Text = "Panel refreshed at: " +
        DateTime.Now.ToLongTimeString(); // Checks if update panel reloads

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        Timer1.Enabled = true; //Starts Timer. It seems to refresh the page. 
    }
}

脚本

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
<script type="text/javascript">
               function delayer() {
        setTimeout (function () {ShowPopUp()}, 15000); 
    }
    delayer();
</script>
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        </div>
        &nbsp; &nbsp;
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" Enabled="true">
                </asp:Timer>
                &nbsp;
                <asp:Label ID="Label1" runat="server" Text="PanelNotRefreshedYet"></asp:Label>&nbsp;&nbsp;
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="ShowPopUp();" />
    </form>
</body>
</html>

System.Web.UI.Timer在启用时刷新页面

我想你很困惑。Timer1是服务器端控件。因此,如果您仍在处理页面,它将在服务器端启动,对客户端没有影响。当它在代码中激发时,页面可能已经呈现,因此您不会看到该Timer1对象的Timer1_Tick事件的影响。由于页面已经完成渲染,您不能注入新的JavaScript、修改页面或类似的操作。请记住,网络开发是一件不相连的事情。你发送一个请求,你就会得到回应。网络本质上没有任何事件。有一些库可以触发事件等等,但我认为这远远超出了你想要实现的目标。

对于客户端的"定时器",您需要使用JavaScriptsetTimeout方法,您已经验证该方法有效,它是实现您想要实现的延迟的正确方法。

setTimeout (function () {ShowPopUp()}, 15000);

如果你仍然想在Alert类中完成,那么去掉Timer1,让Alert类在JavaScript:中注入超时

protected void Button1_Click1(object sender, EventArgs e)
{
    Alert.Show("He heee", this);
}

在Alert中,将脚本更改为:

string script = "<script type='"text/javascript'">setTimeout(function() {alert('" + cleanMessage + "');}, 15000);</script>";

您的按钮正在进行回发,因此是的,页面将被刷新,page_Load函数将再次运行。您应该使用IsPostback属性对此进行测试。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostback) {
        Timer1.Enabled = false;
        Label2.Text = "Panel refreshed at: " +
          DateTime.Now.ToLongTimeString(); // Checks if page reloads
    }
}

您可能希望在页面上使用JavaScript显示警报,而不是在服务器端运行它。

<script type="text/javascript">
     function showPopup()
     {
       alert("Hey, click something already");
      }
           function delayer() {
            setTimeout (showPopUp, 15000); 
               }
delayer();
</script>

把你的信息写成这样。如果你的逻辑很简单,可能会更容易。