Unable to find ASP Repeater child control from ASP

2019-07-28 02:14发布

问题:

See the Image below:

Scenario

I have a repeater control bearing Id "rpt1" ,inside the ItemTemplate of which I have a span control whose Id is "abc", which is currently displaying a number. I also have a 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.

Requirement

What i need is that whenever my second part of the timer reaches 00, I would like to change the content of span from number to some image.

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>
        </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; }
    }
}

Earlier the code was executing on single thread. Now, I tried keeping the threads apart but no avail. I think reloading page each time is main issue. Is it possible that I can only refresh the timer part not the entire page.

回答1:

All you have to do is:

  1. In the ASPX code, move the REPEATER into the UpdatePanel.ContentTemplate, juste below lblNum.
  2. In the C# code, remove the line "t.Suspend();".

I hope this will help you.

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();
        }
    }