0

単純なアプリを開発するために、.NET 4 で ASP.NET と C# を使用しています。いくつかのコントロールを含む項目テンプレートを持つリピーターがあります。そのうちの 1 つは、複雑な計算に応じて設定する必要があるラベルです。次のように、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.";
    }
}

リピーターのデータ ソースにはいくつかの行が含まれているため、リピーターの各インプレッションがイベント ハンドラーを呼び出し、関連する行のデータに従ってメッセージを表示すると予想されます。ただし、最初のリピーター インプレッションに表示されるメッセージは1 つしか表示されませんが、データ ソースの最後の行のデータと一致します。

イベントが発生するたびに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 に答える