asp.netinline-code

Concatenate two or more strings in inline code ASP.NET


I am trying to place a * next to the name based on a condition.

My code :

 <asp:Label ID="lblOne" runat="server"   Text= '<%# Eval("name") + ((Eval("StatusId").Equals(0) && Eval("assignfilename") == null) ? " *" : "") %>' > </asp:Label>

Thanks

BB


Solution

  • If you're pushing the limits of what you can easily handle with inline code, you could always write a function instead. Then you can do something like:

     <asp:Label ID="lblOne" runat="server"   Text= '<%# EmitSomeText(Eval("name"), Eval("StatusId"), Eval("assignfilename")) %>' />
    

    This lets you break a complex expression up into however many lines it needs to be, which can be a little less awkward. You can use a function in your CodeBehind or any other class.

    If you're binding to a class that you have access to, you could add a readonly property. Then you can do something like Eval("MyNewProperty").

    I use that for exposing formatting that I need to re-use. For instance, Customer.CustomerFullName might return last name first seperated be a comma (intelligently handling situations where one or the other or both are missing) plus an optional title, since maybe my customers are medical folks and some of them have PhDs and MDs.