Can I use an IF statement in a GridView ItemTemplate?
C#.NET use the below code
<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>
<asp:Label Text='<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
VB.NET use the below code
<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>
<asp:Label Text='<%# If(Eval("Status").ToString() = "A", "Absent", "Present") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Instead of Eval
you can use any given public function. So you might try and do something like the following:
<ItemTemplate>
<%# (String.IsNullOrEmpty(Eval("Email").ToString()) ? String.Empty : String.Format("<a href='mailto:{0}'>{1}</a>", Eval("Email"), Eval("Name")) %>
</ItemTemplate>
If have not tried the exact syntax, but I'm using something like that in one of my pages.