Go Back   SitePoint Forums > Forum Index > Program Your Site > .NET
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 16, 2003, 18:18   #1
dalangalma
SitePoint Evangelist
 
dalangalma's Avatar
 
Join Date: Aug 2002
Posts: 474
"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?
dalangalma is offline   Reply With Quote
Old Nov 16, 2003, 23:26   #2
freddydoesphp
Dumb PHP codin' cat
 
Join Date: Aug 2000
Location: San Diego, CA
Posts: 5,460
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.


Code:
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";
 }
}
freddydoesphp is offline   Reply With Quote
Old Nov 16, 2003, 23:38   #3
dalangalma
SitePoint Evangelist
 
dalangalma's Avatar
 
Join Date: Aug 2002
Posts: 474
Hmm, I'm not really sure how to do the repeater stuff in codebehind.... here's the full itemtemplate:

Code:
 <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...
dalangalma is offline   Reply With Quote
Old Nov 17, 2003, 02:20   #4
dalangalma
SitePoint Evangelist
 
dalangalma's Avatar
 
Join Date: Aug 2002
Posts: 474
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.
dalangalma is offline   Reply With Quote
Old Nov 17, 2003, 04:24   #5
archigamer
.NET inside
 
archigamer's Avatar
 
Join Date: Jan 2002
Location: Strongsville OH
Posts: 1,588
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?
archigamer is offline   Reply With Quote
Old Nov 17, 2003, 10:42   #6
dalangalma
SitePoint Evangelist
 
dalangalma's Avatar
 
Join Date: Aug 2002
Posts: 474
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?
dalangalma is offline   Reply With Quote
Old Nov 17, 2003, 11:16   #7
freddydoesphp
Dumb PHP codin' cat
 
Join Date: Aug 2000
Location: San Diego, CA
Posts: 5,460
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.
freddydoesphp is offline   Reply With Quote
Old Nov 17, 2003, 11:24   #8
freddydoesphp
Dumb PHP codin' cat
 
Join Date: Aug 2000
Location: San Diego, CA
Posts: 5,460
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"

Code:
<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.


Code:
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";
 }
}
freddydoesphp is offline   Reply With Quote
Old Dec 11, 2006, 12:55   #9
mzirino
SitePoint Member
 
Join Date: Dec 2006
Posts: 1
Great answer Thanks!

Great answer. Thanks!
mzirino is offline   Reply With Quote
Old Dec 11, 2006, 13:32   #10
wwb_99
Community Advisor
silver trophybronze trophy
 
wwb_99's Avatar
 
Join Date: May 2003
Location: Washington, DC
Posts: 9,260
Good answer freddy. But you can actually do this without using a lick of codebehind.

Just do something like this in your repeater:

Code:
<ItemTemplate>
<li class='<%# ((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.
wwb_99 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 16:42.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved