I am trying to do this in a repeater:
<%# Iif((int)DataBinder.Eval(Container.DataItem, "Id") == SelectedJobDefId, "blue", "red")%>
Problem is that the member property only evaluates if this syntax is used:
<%= SelectedJobDefId %>
DataBinder.Eval() only works if the hash symbol is used.
As a test, I tried this:
<%= SelectedJobDefId %>
<%# DataBinder.Eval(Container.DataItem, "Id") + " " + SelectedJobDefId %>
The first SelectedJobDefId stays correct as I switch rows (LinkButton event).
The DataBinder part is correct for each row.
The 2nd output of SelectedJobDefId is always “1”.
How can I compare these two values?
UPDATE:
Based on the suggestion by Jeremy, I made the td a server tag and tried to do the logic in OnItemDataBound like this:
protected void Repeater2_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
DataRowView drv = (DataRowView)item.DataItem;
int id = (int)drv.Row.ItemArray[0];
HtmlTableCell td = (HtmlTableCell)item.FindControl("failtd");
if (id == this.SelectedJobDefId){td.BgColor = "green";}
}
When I step through the debugger, the OnItemDataBound event is happening before the LinkButton event that updates the SelectedJobDefId property which explains the default value of 1.
Is there a way to switch the order of events? I may just be taking the wrong approach.
Just putting this out there as an option, though obviously not most efficient approach.
You could explicitly rebind the
Repeaterin theLinkButtonclick handler, afterSelectedJobDefIdhas been updated. In this way, you should be able to keep all the aspx markup you originally had.