Search code examples
asp.netgridviewinline-code

populate lable control inside a gridview based on inline Conditional statement


I am trying to display a plain text in the column(contains a label) of gridview based on a condition. Here is my erroneous code. Please correct.

 <asp:Label ID="lblAsgn" runat="server"   Text= '<%#Eval("StatusId") == 0 ? "NEW" : "OLD" %>' > </asp:Label>

Thanks in advance.

BB


Solution

  • <asp:Label 
        ID="lblAsgn" 
        runat="server"   
        Text='<%# FormatText(Eval("StatusId")) %>' />
    

    where FormatText could be a method in your code behind:

    protected string FormatText(object o)
    {
        int value;
        if (int.Parse(o as string, out value) && value == 0)
        {
            return "NEW";
        }
        return "OLD";
    }