简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM