Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 321
BumbleBee Avatar asked Oct 05 '11 23:10

BumbleBee


People also ask

How do you concatenate two or more strings in C#?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Can you concatenate more than 2 strings?

Besides, the “&” operator has no limitations regarding the number of strings that you can join. In contrast, the CONCATENATE function is limited to 8,192 characters, which means that you can only use it to join up to 255 strings.

Which operator is used to concatenate two or more strings?

The string operator (&) can be used in formulas to concatenate, or join, two or more strings or the contents of referenced cells.

What is the most efficient way to concatenate many strings together?

If you are concatenating a list of strings, then the preferred way is to use join() as it accepts a list of strings and concatenates them and is most readable in this case. If you are looking for performance, append/join is marginally faster there if you are using extremely long strings.


2 Answers

I'm not really familiar with in-line codes and your code seems to be a bit complicated. But I also need to concatenate an Eval("record") and a text. So to answer the question on how to concatenate, ampersand worked for me.

'<%# Eval("name") & " *" %>'

hope this helps anyone.

like image 72
Gellie Ann Avatar answered Sep 20 '22 21:09

Gellie Ann


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.

like image 21
Brian MacKay Avatar answered Sep 18 '22 21:09

Brian MacKay