"if" statement in asp:repeater

I’m making an unordered list using a repeater, and I want to conditionally add some text to my HTML based on the value of one of my datasource fields. Thus, I added a line like this: <%#if(DateTime.Parse(DataBinder.Eval(Container.DataItem, “dateandtime”).ToString()) < DateTime.Now) Response.Write(“class=\“oldevent\””);%> But it dosen’t like that. How do I use an if statement in those little <%# %> blocks?

Well firstly I would move that logic to your code behind file. And I would do this in the Repeater ItemDataBound event.

It looks as if you actually are trying to set the cssclass of a label or something in your RepeaterItem ItemTemplate.

So just change lblIsOld to the id of the label holding the text whose cssclass you want to modify.

Make sure to set the ItemDataBound Event handler for the Repeater Control.


if ( e.Item.ItemType == ListItemType.Item ||
 e.Item.ItemType == ListItemType.AlternatingItem ) 
{
 DataRowView r = (DataRowView)e.Item.DataItem;
 DateTime od = DateTime.Parse(r["dateandtime"].ToString());
 if(od < DateTime.Now)
 {
  Label l = (Label) e.Item.FindControl("lblIsOld");
  l.CssClass = "oldevent";
 }
}

Hmm, I’m not really sure how to do the repeater stuff in codebehind… here’s the full itemtemplate:


 <ItemTemplate>
 	<li<%#if(DateTime.Parse(DataBinder.Eval(Container.DataItem, "dateandtime").ToString()) < DateTime.Now) Response.Write("class=\\"oldevent\\"");%>>
    <%#DateTime.Parse(DataBinder.Eval(Container.DataItem, "dateandtime").ToString()).ToString("dddd, MMM dd, h:mm")%>, 
	<a href="<%#DataBinder.Eval(Container.DataItem, "venueurl")%>"><%#DataBinder.Eval(Container.DataItem, "venuename")%></a>. 
    Price: <%#DataBinder.Eval(Container.DataItem, "price")%>. <%#DataBinder.Eval(Container.DataItem, "comment")%>
  <%#DataBinder.Eval(Container.DataItem, "agelimit")%>
	</li>
</ItemTemplate>
  

I’m not sure how to reproduce that on the codebehind side… I like to keep the HTML and the data I’m inserting into it pretty separate, so I don’t want to be writing the actual HTML from the code side of things…

For example, how do I have a label inside my itemtemplate and then retrieve it? I can’t have a ton of label elements with the same ID in my HTML, so that makes selecting it by ID hard… this whole thing is confusing me.

The <%# cannot be used for logic, that is strictly for databinding. freddydoesphp has pretty much given the answer. You have to execute some coding when the ItemDataBound event fires.

The ItemDataBound event happens when an from your datasoruce (DataTable, DataSet, etc.) is added to your control (in this case a repeater.)

As far as finding the label, freddydoesphp answered that for you too.
Label l = (Label) e.Item.FindControl(“lblIsOld”);

Did you even look at freddy’s code?

Yeah, I actually did, and I understood most of it. However, the FindControl thing bothered me, since it seems to find an item by ID and I’d rather not have multiple items in my XHTML with the same ID. Did you even read my question?

You shouldn’t need multiple items with the same id. The <ItemTemplate> is repeated for each item in the repeater. So one label in the ItemTemplate will work for all the items in the repeater, get it?

The id you specify in the repeater item is for progamatically accessing the controls and should not effect xhtml compliance.

Are you using Visual Studio.NET or notepad?

I now see what you are doing, You have alist item and you want to set the css class on the fly if the entry is older than today right?

So following what I already showed you just set the id of the <li> tag and set it to runat=“server”


<ItemTemplate>
 <li runat="server" id="listItem">
  <%#DateTime.Parse(DataBinder.Eval(Container.DataItem, "dateandtime").ToString()).ToString("dddd, MMM dd, h:mm")%>, 
  <a href="<%#DataBinder.Eval(Container.DataItem, "venueurl")%>"><%#DataBinder.Eval(Container.DataItem, "venuename")%></a>. 
  Price: <%#DataBinder.Eval(Container.DataItem, "price")%>. <%#DataBinder.Eval(Container.DataItem, "comment")%>
  <%#DataBinder.Eval(Container.DataItem, "agelimit")%>
 </li>
</ItemTemplate>

In the code behind.


if ( e.Item.ItemType == ListItemType.Item ||
 e.Item.ItemType == ListItemType.AlternatingItem ) 
{
 DataRowView r = (DataRowView)e.Item.DataItem;
 DateTime od = DateTime.Parse(r["dateandtime"].ToString());
 if(od < DateTime.Now)
 {
  HtmlGenericControl l = (HtmlGenericControl) e.Item.FindControl("lItem");
  l.CssClass = "oldevent";
 }
}


Good answer freddy. But you can actually do this without using a lick of codebehind.

Just do something like this in your repeater:


<ItemTemplate>
<li class='<&#37;# ((DateTime)((DataRowView)Container.DataItem)["dateandtime"])<DateTime.Now ? "olditem" : string.Empyty %>
. . . .
</ItemTemplate>

Other minor note–there is no need to boil the datetime from the datarowview out to a string to bring it back as a date time. You just need to cast it as appropriate.