0

在 page_load 期间,我禁用了计时器。当我按下 Button1 时,我启用了计时器,但页面刷新了。因此,它永远不会到达 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>
4

3 回答 3

0

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

对于客户端“计时器”,您需要使用 JavaScript setTimeout 方法,您已验证该方法可以正常工作,并且是实现您希望实现的延迟的正确方法。

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

如果您仍想在 Alert 类中执行此操作,则摆脱 Timer1 并让您的 Alert 类在 JavaScript 中注入超时:

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

在警报中,将您的脚本更改为:

string script = "<script type=\"text/javascript\">setTimeout(function() {alert('" + cleanMessage + "');}, 15000);</script>";
于 2013-07-09T15:15:46.127 回答
0
<script type="text/javascript">
     function showPopup()
     {
       alert("Hey, click something already");
      }

           function delayer() {
            setTimeout (showPopUp, 15000); 
               }
delayer();
</script>

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

于 2013-07-09T15:28:19.657 回答
0

您的按钮正在回发,所以是的,页面将被刷新,您的 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 显示警报,而不是在服务器端运行它。

于 2013-07-09T14:31:35.157 回答