Ranjith Varadan Ranjith Varadan - 1 month ago 11
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">
<Columns>
<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">
<ItemTemplate>
<a href='<%# Eval("Status", "Questions/{0}.aspx") %>'>'<%# Eval("Status") %>'</a>
<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


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 "
Absent
".

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

Try this:

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

</ItemTemplate>
Comments