|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() 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?
|
|
|
|
|
|
#2 |
|
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";
}
}
|
|
|
|
|
|
#3 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() 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>
|
|
|
|
|
|
#4 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() 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.
|
|
|
|
|
|
#5 |
|
.NET inside
![]() ![]() ![]() ![]() ![]() ![]() 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? |
|
|
|
|
|
#6 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() 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?
|
|
|
|
|
|
#7 |
|
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. |
|
|
|
|
|
#8 |
|
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>
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";
}
}
|
|
|
|
|
|
#9 |
|
SitePoint Member
Join Date: Dec 2006
Posts: 1
|
Great answer Thanks!
Great answer. Thanks!
|
|
|
|
|
|
#10 |
|
Community Advisor
![]() ![]() ![]() 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> |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 16:42.












Hybrid Mode
