• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

How can I use a conditional with DataBinder.Eval?

I have a small app that loads content from an XML file into a repeater.  One of these pieces from XML is called "author".  Right now when I use the attached code, I get the author as it's listed in XML -- a value like "jcoulson" or "tbarlow".  What I'd like to do is include some sort of conditional there that specifies "if author is jcoulson, print Jeremy Coulson".  I know it's possible to use conditionals within DataBinder.Eval, but I don't get how.
<asp:Repeater ID="rptStatus" runat="server">
	<ItemTemplate>
		<span class="title">Latest IT Status</span><br /><br />
        <span class="head"><%# DataBinder.Eval(Container.DataItem, "title")%></span><br />
        <span class="time"><%# String.Format("{0:g}", DataBinder.Eval(Container.DataItem, "pubDate"))%></span><br />           
        <span class="body"><%# DataBinder.Eval(Container.DataItem, "content")%>
			Posted by <%# DataBinder.Eval(Container.DataItem, "author")%>.
        </span>
    </ItemTemplate>
</asp:Repeater>

Open in new window

0
mrcoulson
Asked:
mrcoulson
  • 3
  • 2
1 Solution
 
amar31282Commented:
Use Method below if you want to make check for some particular name...



if requirement is different the please provide more details


<asp:Repeater ID="rptStatus" runat="server">
        <ItemTemplate>
                <span class="title">Latest IT Status</span><br /><br />
        <span class="head"><%# DataBinder.Eval(Container.DataItem, "title")%></span><br />
        <span class="time"><%# String.Format("{0:g}", DataBinder.Eval(Container.DataItem, "pubDate"))%></span><br />           
        <span class="body"><%# DataBinder.Eval(Container.DataItem, "content")%>
                        Posted by <%# DataBinder.Eval(Container.DataItem, "author").ToString()=="jcoulson"?"Jeremy Coulson":DataBinder.Eval(Container.DataItem, "author").ToString() %>.
        </span>
    </ItemTemplate>
</asp:Repeater>

Open in new window

0
 
mrcoulsonAuthor Commented:
Bingo!  It's a LONG LINE of code, but it works.

Thanks!

Jeremy
Posted by <%# DataBinder.Eval(Container.DataItem, "author").ToString()=="tbarlow"?"Tim Barlow":DataBinder.Eval(Container.DataItem, "author").ToString()=="jcoulson"?"Jeremy Coulson":DataBinder.Eval(Container.DataItem, "author").ToString()=="mlemaste"?"Marcus Lemasters":DataBinder.Eval(Container.DataItem, "author").ToString()=="pfly"?"Patrick Fly":DataBinder.Eval(Container.DataItem, "author").ToString()=="mnugent"?"Michelle Nugent":DataBinder.Eval(Container.DataItem, "author").ToString()=="acahill"?"Annie Cahill":DataBinder.Eval(Container.DataItem, "author").ToString()=="btyson"?"Benny Tyson":DataBinder.Eval(Container.DataItem, "author").ToString()=="epeng"?"Hsuan-Jung Peng":DataBinder.Eval(Container.DataItem, "author").ToString()=="ndunleav"?"Nina Dunleavy":DataBinder.Eval(Container.DataItem, "author").ToString() %>

Open in new window

0
 
amar31282Commented:
hmm

its working but in your implementation its not good practice the way u r implementing it.

Infact there should be two columns/or one which have complete name and that goes into author..

Also you can make server side function and call it here for the author that would be much better

thnks
0
 
mrcoulsonAuthor Commented:
Right, I figured it was bad programming practice to do it this way, but a haphazard solution works just fine here.

I don't have control over what's in the XML file, so I needed a way to programmatically get from jcoulson to Jeremy Coulson.  Having an author and author_full_name elements would be better, but I do what I can with what I have.

Jeremy
0
 
amar31282Commented:
thats fine sometimes we have to compromise

Cheers...
Regards,
Amarjit Singh Dhunna
Web Developer,
India
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now