Ajax计时器:仅执行5次

本文关键字:执行 5次 计时器 Ajax | 更新日期: 2023-09-27 18:19:47

我有一个ajax Timer控件。它将"+"添加到标签的文本值中。此计时器只能工作五次,间隔为"1000",即只能使用五个"+"。之后,lblPostbackType应使用计数进行更新。我该如何实现它?

public partial class _Default : System.Web.UI.Page
{
static int partialPostBackCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
        {
           partialPostBackCount = partialPostBackCount + 1;
           lblPostbackType.Text = "Partial Postback:: " + partialPostBackCount.ToString();
        }
        else
        {
            lblPostbackType.Text = "Full Postback";
        }
    }
}
protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = Label1.Text + "+";
}
}

设计者代码是

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" ID="Timer1" Interval="1000" OnTick="Timer1_Tick" />
 <asp:Label runat="server" ID="lblPostbackType" >SAMPLE</asp:Label>
<asp:UpdatePanel runat="server" ID="TimePanel" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Label runat="server" ID="Label1" />
    </ContentTemplate>
</asp:UpdatePanel>
</form>

感谢

Ajax计时器:仅执行5次

尝试这个

protected void Timer1_Tick(object sender, EventArgs e)
{

    if (partialPostBackCount > 5 )
        {
           lblPostbackType.Text = "Partial Postback:: " + 
                                   partialPostBackCount.ToString();
           //Timer1.Enabled = false; //if you don't want it to continue
        }
        else
        {
            partialPostBackCount = partialPostBackCount + 1;
            Label1.Text = Label1.Text + "+";
        }
}

*不建议使用静态变量。。

保持Page_Load事件方法干净。。