1

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.