What is the best way to use multiple EVAL fields in a GridView ItemTemplate?
Looking to have some control over formatting for appearance as well as setting up hyperlinks/javascript etc.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Even clearer, IMO, is:
<%# String.Format("{0} - {1}", Eval("Name1"), Eval("Name2")) %>
Method 2
I had previously used this (bad, I know):
<%# Eval("Name1", "{0} - ")%> <%#Eval("Name2")%>
Result = ‘John – Smith’
But just discovered that I can also put TWO (or more) Evals in the same data-bound group:
<%#Eval("Name1") & " - " & Eval("Name2")%>
Result = ‘John – Smith’
Or
<%# "First Name - " & Eval("Name1") & ", Last Name - " & Eval("Name2")%>
Result = ‘First Name – John, Last Name – Smith’
Method 3
Eval and Bind both suck.
Why get the property through reflection? You can access it directly like this:
((MyObject)Container.DataItem).MyProperty
It’s not like the object is unknown to you at runtime. That’s my two cents, anyhow.
Method 4
I have a easiest way to do this same thing…
<asp:Label ID="lblName" runat="server" Text='<%#Eval("FirstName").ToString() +", "+ Eval("LastName").ToString() %>'></asp:Label>
.
<%#Eval("FirstName").ToString() +", "+ Eval("LastName").ToString() %>
Here both objects are converted into string the concatenate them.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0