0

我在 .NET 4 上使用 ASP.NET 和 C# 来开发一个简单的应用程序。我有一个带有包含一些控件的项目模板的中继器;其中之一是必须根据复杂计算设置的标签。我正在使用该OnItemDataBound事件来计算文本并在后面的代码中设置标签的文本,如下所示:

protected void repRunResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //capture current context.
    Repeater repRunResults = (Repeater)sender;
    Label laMessage = (Label)repRunResults.Controls[0].FindControl("laMessage");
    DSScatterData.RunResultsRow rRunResults = (DSScatterData.RunResultsRow)((DataRowView)(e.Item.DataItem)).Row;

    //show message if needed.
    int iTotal = this.GetTotal(m_eStatus, rRunResults.MaxIterations, rRunResults.TargetLimit);
    if(iTotal == 100)
    {
        laMessage.Text = "The computed total is 100.";
    }
    else
    {
        laMessage.Text = "The computed total is NOT 100.";
    }
}

我的转发器的数据源包含几行,因此我希望转发器的每次印象都会调用事件处理程序并根据关联行中的数据显示消息。但是,我只收到一条消息,该消息出现在第一次重复印象中,但与数据源中最后一行的数据相匹配。

似乎每次ItemDataBound事件触发时,我的代码捕获的控件都是相同的,因此我在转发器的每次印象中都会覆盖消息。我已经逐步完成了代码,这显然是正在发生的事情。

知道为什么吗?以及如何解决?

笔记。我的中继器嵌套在另一个中继器中。我认为这不应该是相关的,但它可能是相关的。

4

1 回答 1

2

你正在抢第一个。您需要像这样使用传入的项目:

protected void repRunResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //capture current context.
    Repeater repRunResults = (Repeater)sender;
    Label laMessage = e.Item.FindControl("laMessage"); //<-- Used e.Item here
    DSScatterData.RunResultsRow rRunResults = (DSScatterData.RunResultsRow)((DataRowView)(e.Item.DataItem)).Row;

    //show message if needed.
    int iTotal = this.GetTotal(m_eStatus, rRunResults.MaxIterations, rRunResults.TargetLimit);
    if(iTotal == 100)
    {
        laMessage.Text = "The computed total is 100.";
    }
    else
    {
        laMessage.Text = "The computed total is NOT 100.";
    }
}
于 2014-05-13T22:59:07.460 回答