无法从ASP Timer tick事件中找到ASP Repeater子控件

时间:2013-05-28 09:35:45

标签: c# asp.net .net .net-4.0 timer

请参见下图:

enter image description here方案

我有一个转发器控件,带有ID“ rpt1 ”,在ItemTemplate中我有一个跨度控制,其ID为“< strong> abc “,目前正在显示一个数字。我也有ticker above the repeater in format dd-MM-yyyy hh:mm:ss (27-05-2013 11:24:36), made up of ASP:Timer and ASP:UpdatePanel

要求

我需要的是,每当我的计时器的第二部分达到00时,我想将跨度的内容从数字改为某些图像。

代码

ASPX:

 <form id="form1" runat="server">
    <asp:ScriptManager ID="scr" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="timer" runat="server" Interval="1000" OnTick="timer_Tick">
    </asp:Timer>
    <asp:UpdatePanel runat="server" ID="up1" UpdateMode="Always">
        <ContentTemplate>
            <asp:Label runat="server" ID="lblNum"></asp:Label>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:Repeater runat="server" ID="rpt1">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <span id="abc" runat="server">
                            <%#Eval("Number") %></span>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>
    </form>

CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;

namespace WebTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindList();
        }

        protected void timer_Tick(object sender, EventArgs e)
        {
            lblNum.Text = string.Format("{0:dd:MM:yyyy hh:mm:ss}",DateTime.Now);

            if (DateTime.Now.Second == 30)
            {
                Thread t = new Thread(DisplayImages);
                t.Start();
                t.Suspend();
                t.Join();

            }
        }

        private void BindList()
        {
            var list = new List<Numbers>();
            list.Add(new Numbers() { Number = 5 });
            list.Add(new Numbers() { Number = 6 });
            list.Add(new Numbers() { Number = 7 });
             list.Add( new Numbers() { Number = 5 });
             list.Add(new Numbers() { Number = 5 });

            rpt1.DataSource = list;
            rpt1.DataBind();
        }

        private void DisplayImages()
        {

            foreach (RepeaterItem item in rpt1.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    var checkBox = ((HtmlGenericControl)(item.FindControl("abc")));

                    checkBox.InnerHtml = "<img src='http://pra.aps.org/sites/pra.test.ridge.aps.org/themes/PRA/graphics/button_go.gif'/>";
                }
            }
        }
    }

    public class Numbers
    {
        public int Number { get; set; }
    }
}

之前的代码是在单线程上执行的。现在,我试图保持线程分开,但没有用。我认为每次重新加载页面都是主要问题。是否有可能只刷新计时器部分而不是整个页面。

1 个答案:

答案 0 :(得分:0)

您所要做的就是:

  1. 在ASPX代码中,将REPEATER移动到UpdatePanel.ContentTemplate,juste低于lblNum。
  2. 在C#代码中,删除“t.Suspend();”。
  3. 我希望这会对你有所帮助。

    <强> CODE

    ASPX:

    <form id="form1" runat="server">
    <asp:ScriptManager ID="scr" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="timer" runat="server" Interval="1000" OnTick="timer_Tick">
    </asp:Timer>
    <asp:UpdatePanel runat="server" ID="up1" UpdateMode="Always">
        <ContentTemplate>
            <asp:Label runat="server" ID="lblNum"></asp:Label>
            <asp:Repeater runat="server" ID="rpt1">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <span id="abc" runat="server">
                            <%#Eval("Number") %>
                        </span>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>
    

    CS:

        protected void timer_Tick(object sender, EventArgs e)
        {
            lblNum.Text = string.Format("{0:dd:MM:yyyy hh:mm:ss}", DateTime.Now);
    
            if (DateTime.Now.Second == 30)
            {
                Thread t = new Thread(DisplayImages);
                t.Start();
                //t.Suspend();
                t.Join();
            }
        }