IF statement in a Repeater

I already made a research but still I can’t solved this problem of mine. I have an announcement page in which I will allow the author to edit or even delete his posting. However I cannot view the links (i.e. EDIT and DELETE) inside a repeater. If the the value of the Session is equal to the value of the field postedby then I am going to display the said links. Below is the code I am trying to fix. Please help me, I am stuck for two day just because of this page.


<asp:Repeater ID="rptAnnouncement" runat="server" DataSourceID="dtAnnouncement">
        <HeaderTemplate>
            <table style="margin-top: 5px;">
        </HeaderTemplate>
        <ItemTemplate>
            <tr style="font-weight: bold; color: #336699; font-size: 14px; margin-top: 5px;">
                <td>
                    <a class="announcementlink" href='<%# "announcement.aspx?id=" +  DataBinder.Eval(Container.DataItem,"id") %>'>
                    <%# DataBinder.Eval(Container.DataItem, "title") %></a>
                </td>
            </tr>
            <tr style="color: #AAA; font-size: 10px;">
                <td>
.                    Posted On: <%# DataBinder.Eval(Container.DataItem, "dateposted") %>
                </td>
            </tr>
            <tr>
                <td><%# DataBinder.Eval(Container.DataItem, "contents") %></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
        </asp:Repeater>

you can use something like this form in the repeater.

if(condition) ? a : b

Can you give me an idea how to apply the example that you had cited.

use a hyperlink control and set the Visible property to the boolean condition to show or hide the hyperlink

<asp:HyperLink … Visible='<%# DataBinder.Eval(Container.DataItem,“postedby”) == Something %>

Don’t forget the “return true” (or, “return false” if it is incorrect) in the ternary condition.

Now, I got this error…The name ‘Container’ does not exist in the current context.

How does your code look?

I haven’t used a repeater in so long, I may be forgetting something, but I seem to remember just using Eval(). Maybe that was just for asp:ListView though.

What happens if you use the following link code instead?

<a class=“announcementlink” href=‘<%# “announcement.aspx?id=” + Eval(“id”) %>’><%# Eval(“title”) %></a>

What I am trying to do is to set the visible property of a linkbutton based from the given example of one of the member here in this forum. If the value of my session is equal to the value of the of content of my datasource then I will the hyperlink will be visible.


<asp:HyperLink  runat="server" Visible='<%# DataBinder.Eval(Container.DataItem,"postedby") == Session["userid"].ToString()) %>

Try using the following just to see what the values actually are. I have the suspicion that one of them isn’t outputting the right textual value.

<p><%# DataBinder.Eval(Container.DataItem,“postedby”) == Session[“userid”].ToString()) %></p>

<p><%# DataBinder.Eval(Container.DataItem,“postedby”) %></p>

<p><%=Session[“userid”].ToString()</p>

What you may need to do is cast the entire thing to string. And while you’re there toss in the use of Equals to make it read better.

<p><%# DataBinder.Eval(Container.DataItem, “postedby”).Equals(Sesson[“userid”]).ToString() %></p>

This compares the two underlying values and returns a bool, which is then cast to a string. Should output either ‘true’ or ‘false’.

If this doesn’t point you in the right direction, then I’m out of ideas.

Good luck! =)

You need to convert your expression to string as well:

<p><%# (DataBinder.Eval(Container.DataItem,“postedby”).ToString() == Session[“userid”].ToString()) %></p>

Thanks to both of you guys. I preferred to used the code


<%# DataBinder.Eval(Container.DataItem, "postedby").Equals(Sesson["userid"]).ToString() %>

It works and I am not experiencing a null object during the loading of my page.