dragonfly
dragonfly

Reputation: 17773

Conditional output in GridView row

I databing array of User objects to GridView control. Last column contains "action" anchors (edit, remove):

    <asp:TemplateField HeaderText="Actions">
        <ItemTemplate>
            <a href="Remove.aspx?id=<%# Eval("user_id") %>">Remove</a>
            <a href="Edit.aspx?id=<%# Eval("user_id") %>">Edit</a>
    </ItemTemplate>
    </asp:TemplateField>

However I would like not to output first anchor to Remove action if currently binded User object has the same id as use logged in (available with this.SessionUser.Id).Something like this:

    <asp:TemplateField HeaderText="Actions">
        <ItemTemplate>
            <a href="Remove.aspx?id=<%# Eval("user_id") %>">Remove</a>
            if (this.SessionUser.Id <> Eval("user_id") { <a href="Edit.aspx?id=<%# Eval("user_id") %>">Edit</a> }
    </ItemTemplate>
    </asp:TemplateField>

How can I do it?

Thanks!

Upvotes: 1

Views: 616

Answers (3)

Ruben
Ruben

Reputation: 15515

You can use a runat="server" control for this

<asp:TemplateField HeaderText="Actions">
    <ItemTemplate>
        <a href="Remove.aspx?id=<%# Eval("user_id") %>">Remove</a>
        <a href="Edit.aspx?id=<%# Eval("user_id") %>" runat="server"
           visible='<%# this.SessionUser.Id <> Eval("user_id") %>'>Edit</a>
    </ItemTemplate>
</asp:TemplateField>

All server controls, even HTML tags with runat="server" have this Visible property, which omits the control from the final HTML when it is false.

Upvotes: 3

roman m
roman m

Reputation: 26531

you can use CSS:

<a style='visible:<%# this.SessionUser.Id <> Eval("user_id") %>' > ... </a>

make sure that this.SessionUser.Id is a public variable in your .cs file

Upvotes: 1

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

not supported :( you need to write another function passing it user_id and get the appripriate string from it like this:

 //in cs file
 protected string GetLink(object o)
 {
     if(!SessionUser.Id.Equals(o)) //or anyother way to compare equality
        return string.Format("<a href=\"Edit.aspx?id={0}\">",0);
     return "";
 }

 //in aspx file

 <ItemTemplate>
        <a href="Remove.aspx?id=<%# Eval("user_id") %>">Remove</a>&nbsp;
        <%# GetLink(Eval("user_id"))%>
 </ItemTemplate>

Upvotes: 1

Related Questions