Combining two or more lines in ASP.NET inline code

I am trying to put * next to a conditional name.

My code is:

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

thanks

BB

+6
source share
6 answers

If you push the limits of what you can easily handle with inline code, you can always write a function. Then you can do something like:

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

This allows you to break down a complex expression into any number of lines, which should be, which can be a little less inconvenient. You can use the function in CodeBehind or any other class.

If you bind to a class that you have access to, you can add the readonly property. Then you can do something like Eval ("MyNewProperty").

I use this to display formatting that I need to reuse. For example, Customer.CustomerFullName can return a surname, first separated by a comma (it is wise to handle situations when one or the other or both are missing), plus an optional title, because my clients may be medical workers, and some of them have doctors and doctors.

+3
source

I am not very familiar with embedded codes, and your code seems a bit complicated. But I also need to combine Eval ("record") and text. Therefore, to answer the question of how to unite, the ampersand worked for me.

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

Hope this helps anyone.

+4
source

For simple one-time scripts, the code function works fine.

You can also consider encoding them as properties in the base object.

For example, if the generated text will be used in more than one instance, you will need to code the function several times with Evals in different forms or controls.

I would create a property on a data object, for example. NameWithStatusStar, then your label can be attached directly to the property with the code inside Eval ("NameWithStatusStar")

This is more descriptive and reusable than a series of expressions, plus it’s easier to change (for example, add another field, change the formula, etc.)

+3
source

You can do it as follows:

 Text='<%#"CustomText "+Eval("Name")%>' 
+2
source
 Text='<%#String.Concat(Eval("UserId"), Eval("Username")) %>' 

This worked for me in my project. Found here:

Combine text with Eval

+1
source
  Text='<%# string.Concat(Eval("FirstName"), " ", Eval("LastName"))%>' 

This worked for me in my project. Found here:

Combine text with Eval

-1
source

Source: https://habr.com/ru/post/898713/


All Articles