I need to create buttons for each item in a DataList, but the last button needs to be formatted differently, so it needs a different css class applied to it.
I think this should be done in the OntemDataBound method, but feel free to correct me if I’m wrong.
I want to do something like this:
protected void dlDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemIndex == dlDataList.Items.Count - 1) //This doesn't work like I'd hoped
{
Panel button = (Panel)e.Item.FindControl("btnButton");
button.CssClass = ("altClass");
}
...
}
The problem is, I don’t think the datalist knows how many items it will have in the ItemDataBound event because dlDataList.Items.Count is always the same as the ItemIndex.
Any ideas on how I can give the last button the altClass css class?
Before you bind the DataList, save the total number of items in a page level variable. Then on your ItemDataBound, check to see if the current index is equal to the total items (-1 of course) and set your css class accordingly