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

时间:2013-07-09 14:18:51

标签: c# asp.net ajax

在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.
        }
    }
}

Page Class

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>

3 个答案:

答案 0 :(得分: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显示警报,而不是在服务器端运行。

答案 1 :(得分: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);
}

在Alert中,将脚本更改为:

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

答案 2 :(得分:0)

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

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

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