views:

4487

answers:

4

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.

+1  A: 

I think it would have been better if you posted this as a question and an answer. Just my two cents.

Geoffrey Chetwood
Good call. I've split it out into a question and submitted the rest as an answer.
David HAust
+1  A: 

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'

David HAust
+6  A: 

Even clearer, IMO, is:

<%# String.Format("{0} - {1}", Eval("Name1"), Eval("Name2")) %>
Forgotten Semicolon
+2  A: 

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.

Esteban Araya