Ranjith Varadan Ranjith Varadan - 1 year ago 110
ASP.NET (C#) Question

How to insert a hyperlink inside a ternary operator

I have created a new project in asp.net and i'm struggling to insert a hyperlink inside a ternary operator.

Here is code snippet:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
<a href='<%# Eval("Status", "Questions/{0}.aspx") %>'>'<%# Eval("Status") %>'</a>
<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>

In this above code, if the Status is "A", i need to insert the hyperlink (
<a href='<%# Eval("Status", "Questions/{0}.aspx") %>'>'<%# Eval("Status") %>'</a>
) in the place of "

Here is my Output:

enter image description here

How can i do this? Any advice would be healpful. Thank you.

Update #1:

I need only the absent to be the link, for present, i'm just displaying it as a plain text.

enter image description here

Answer Source

Try this:

  <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='Questions/Absent.aspx' 
       Text='<%# Eval("Status") %>' Visible='<%# Convert.ToBoolean(Eval("Status").ToString() == "A" ? "True" : "False")) %>'>
  <asp:Label ID="Label1" runat="server" Text='Present' Visible='<%#  Convert.ToBoolean((Eval("Status").ToString() == "A" ? "False" : "True")) %>'>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download