Koo SengSeng
Koo SengSeng

Reputation: 953

ASP Repeater Previous Item's DataItem in ItemDataBound is null

I have following code for Repeater databound:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    string strPrevDisplayColumn = "";
    string strCurrentDisplayColumn = "";
    if (e.Item.ItemIndex != 0)
    {
        RepeaterItem previousRepeaterItem = rptCustSales.Items[e.Item.ItemIndex - 1];

        // cannot get value
        strPrevDisplayColumn = Convert.ToString(DataBinder.Eval(previousRepeaterItem.DataItem, "DisplayColumn")) 

    }
    strCurrentDisplayColumn = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "DisplayColumn"));

    if (strCurrentDisplayColumn == strPrevDisplayColumn)
    {
        // Do something
    }
}

I need to check and compare the value of current repeater item's value with previous repeater item's value. But the DataItem of previous rep[eater item is empty.

What did I do wrong here?

Upvotes: 0

Views: 1215

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You do not have access to the DataItem once it is bound to the Repeater. So if you want to compare values you have to store the value in a variable instead of looking for the previous bound item.

int strPrevDisplayColumn = 0;

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataRowView item = e.Item.DataItem as DataRowView;
    int strCurrentDisplayColumn = Convert.ToInt32(item["DisplayColumn"]);

    if (strCurrentDisplayColumn == strPrevDisplayColumn)
    {
        // Do something
    }

    strPrevDisplayColumn = strCurrentDisplayColumn;
}

Upvotes: 1

Related Questions