Databinding Tips: Nesting Eval Statements

Maybe this is obvious, but it wasn’t obvious to me. I’m binding some data in a repeater that has the following output based on two numeric columns in my database. It doesn’t matter why or what the data represents. It’s just two pieces of data with some formatting:

42, (123)

Basically these are two measurements. Initially, I would databind this like so:

<%# Eval("First") %>, (<%# Eval("Second") %>)

The problem with this is that if the first field is null, I’m left with this output.

, (123)

Ok, easy enough to fix using a format string:

<%# Eval("First", "{0}, ") %>(<%# Eval("Second") %>)

But now I’ve learned that if the first value is null, the second one should be blank as well. Hmm... I started to do it the ugly way:

<%# Eval("First", "{0}, ") %> <%# Eval("First").GetType() == 
  typeof(DBNull) ? "" : Eval("Second", "({0})")%>

*Sniff* *Sniff*. You smell that too? Yeah, stinky and hard to read. Then it occured to me to try this:

<%# Eval("First", "{0}, " + Eval("Second", "({0})")) %>

Now that code smells much much better! I put the second Eval statement as part of the format string for the first. Thus if the first value is null, the whole string is left blank. It’s all or nothing baby! Exactly what I needed.

What others have said

Requesting Gravatar... Scott Muc Apr 12, 2007 11:50 AM
# re: Databinding Tips: Nesting Eval Statements
Great tip! I've found myself checking for NULL in my ascx/aspx files and always thought it seemed wrong. Your way looks nice and elegant.
Requesting Gravatar... Kevin Dente Apr 12, 2007 12:33 PM
# re: Databinding Tips: Nesting Eval Statements
Hmm...shouldn't that bit o' logic be in a presenter, so it can be unit tested? ;)
Requesting Gravatar... Karthik Apr 12, 2007 12:56 PM
# re: Databinding Tips: Nesting Eval Statements
I always end up putting such wacky formatting for repeaters inside a codebehind method that simply outputs the string I want.

That way rather than fix it in multiple places when my client inevitably asks to change the format its easy to maintain.

Case in point, I was using "N/A" to denote blank and my client wanted it changed to an image. Because the return of N/A was wrapped in a method I just had to change the method to return an <img> tag.
Requesting Gravatar... Haacked Apr 12, 2007 1:22 PM
# re: Databinding Tips: Nesting Eval Statements
@Kevin: Well there's a fine line. For formatting stuff, especially stuff that changes a lot, I don't like to put it in code. For example, I don't unit test my CSS. I don't consider this "logic" as much as formatting. Though it really straddles the line.

@Karthik: I wanted to avoid having to write a bunch of one-off methods that would require me to recompile the app just for a minor formatting change.
Requesting Gravatar... Kevin Dente Apr 12, 2007 2:09 PM
# re: Databinding Tips: Nesting Eval Statements
Phil,
I hear ya. For me, when I hear "if x then y else z", I think "logic", and I'd rather have it tested. Plus, I'd rather capture that requirement ("the second thing isn't displayed if the first thing isn't") somewhere - the natural place being a test.

But I agree, it's a fine line.
Requesting Gravatar... Haacked Apr 12, 2007 2:22 PM
# re: Databinding Tips: Nesting Eval Statements
Well everything is an "if" statement.

"If this div has a css class of blah, then make the background fuscia."

;)
Requesting Gravatar... Kevin Dente Apr 12, 2007 2:34 PM
# re: Databinding Tips: Nesting Eval Statements
That's it, I'm going to go start writing CSSUnit.

:P
Requesting Gravatar... Scott Muc Apr 12, 2007 3:01 PM
# re: Databinding Tips: Nesting Eval Statements
A CSSUnit would be a godsend! Imagine writing CSSUnit tests that spec out a layout and dimensions and then a test would fail because you forgot a child element inherited some padding you placed on some outer element. One can dream I guess...
Requesting Gravatar... Haacked Apr 12, 2007 3:07 PM
# re: Databinding Tips: Nesting Eval Statements
Setting up the CSSUnit tests would be as time consuming as writing the CSS itself.

I can see it now.

Client: "Wait, I want you to move that two pixels to the left. Hmm.... No. Let's move it one pixel to the right and double the border. Hmm... Now that I see it. No, let's make that background slightly more yellow and move it right one pixel to where it was."

You: "AAAAAAGGHHH!!!!" *rips hair out of head*

The only CSS Unit test that would be useful is perhaps generating a test off of a Photoshop layout. But then, you'd probably never pass that test.

Client: "Hey, the text isn't antialiased like the photoshop mockup. I want it to look *exactly* like the mockup."

You: "AAAAAAAAIIEEEEEEEEEEEEE!!!" *staples hand with stapler*
Requesting Gravatar... Kevin Dente Apr 12, 2007 4:31 PM
# re: Databinding Tips: Nesting Eval Statements
Clearly those are "acceptance" tests. That's another thing entirely. :P

The best way to automate those are...a boot to the head.
Requesting Gravatar... German Rumm Apr 13, 2007 4:00 AM
# re: Databinding Tips: Nesting Eval Statements
I like you "ugly" solution more. It clearly expresses logic - "if first value is not null, then display second"
I wouldn't use a ternary there, simple "if" would be much better.

But your solution is much prettier, yes.
Requesting Gravatar... kevin Apr 13, 2007 5:55 AM
# re: Databinding Tips: Nesting Eval Statements
I agree with Karthik - I have a utility class that handles this just in case I need to make across the board changes - its all done in one place.

I here what phil is saying about having to recompile though as well.

this is where the design decisions come into playe.

for me, the utility methods are both easier to read and provide more "utility" because how often do you need to change something that is pretty much a standard - ie if its empty then display "na", for example.

love the blog!
Requesting Gravatar... Dflying Chen Apr 13, 2007 6:14 AM
# 本周ASP.NET英文技术文章推荐[04/08 - 04/14]
摘要本期共有6篇文章:ASP.NET编译问题的公开Hotfix补丁期待下个版本AjaxPro的发布在ASP.NET2.0中使用MultiView控件实现多页面表单数据绑定的技巧:嵌...
Requesting Gravatar... Filini Apr 13, 2007 7:32 AM
# re: Databinding Tips: Nesting Eval Statements
I, too, prefer to use a separate layer to prepare my data for layout.

In the project I work at the moment, the Data returned by my services is transformed in a DataTable with all the right "strings" for layout presentation, and this DataTable can be databound to a Repeater, or a DataList, or a DataGrid depending on the situation.

I found this approach very easy to mantain, over the years.
Requesting Gravatar... Samyi Apr 13, 2007 9:41 AM
# re: Databinding Tips: Nesting Eval Statements
Good!
Requesting Gravatar... Dflying Chen Apr 13, 2007 6:50 PM
# 本周ASP.NET英文技术文章推荐[04/08 - 04/14](附赠自弹超级玛丽主题曲)
本期共有6篇文章:
Requesting Gravatar... shoutor Apr 14, 2007 7:55 AM
# 本周ASP.NET英文技术文章推荐
ASP.NET编译问题的公开Hotfix补丁 <br />期待下个版本AjaxPro 的发布 <br />在ASP.NET 2.0中使用MultiView控件实现多页面表单 <br />数据绑定的技巧:嵌套Eval语句 <br />在ASP.NET 2.0中访问并更新数据:使用数据源控件以编程方式访问数据 <br />ADO.NET连接池一瞥
Requesting Gravatar... ljianl Apr 15, 2007 6:35 PM
# Databinding Tips: Nesting Eval Statements
这个 有点新鲜 ,呵呵 。。。
Requesting Gravatar... lb Apr 15, 2007 10:59 PM
# re: Databinding Tips: Nesting Eval Statements
>that code smells much much better!

You think !?!?!

>looks nice and elegant.
?? Whu!?

you're all blind! crazy! wacko! that code is stinky! wrong! nasty, tricksy! and i don't like it a heck of a lot.

>I don't consider this "logic" as much as formatting

huh?? sounds like humpty-dumpty way of thinking doesn't it?

CssUnit -- interesting idea ;-)
Requesting Gravatar... David Crowell Apr 16, 2007 10:53 AM
# re: Databinding Tips: Nesting Eval Statements
I rarely use databinding, so I was unaware that null would work that way in a bound control. I like it. I too have been resorting to helper methods in the page class.

Thanks for the tip.
Requesting Gravatar... 沧海依粟 Apr 16, 2007 11:07 AM
# [翻译][收藏].Net数据绑定技巧:嵌套 Eval 综述
Requesting Gravatar... Jason Apr 26, 2007 4:55 PM
# re: Databinding Tips: Nesting Eval Statements
CSSUnit, hmmm, good luck on that one! IE6 quirks mode, IE7 partially fixes quirks mode but still has issues like height problem with floats etc. Just firing up a microsoft browser would fail the tests!...

but the test names would be cool.

AssertThatFirefoxCorrectlyRendersPngAlphaCorrectForBackGroundDivsAndThatIE6FailsMiserablyYetIe7SortaWorksSometimes()
TestThatQuirksModeIe6WorksButNowSomeQuirksWereFixedInIe7SoTestAnotherHackCssTrickForMicrosoftIncompetenceInStandards()

Css and browser compliance is black magic, and only Amazonian Voodoo doctors should attempt to unit test it.
Requesting Gravatar... StefanVE May 15, 2007 6:57 AM
# re: Databinding Tips: Nesting Eval Statements
I usually start out with Evals in the aspx. Then things get more complex and I decorate a property to handle the logic.
Next I start thinking I am having too many Evals so I start using the code behind model (as you only have to cast the object once while Eval performs a cast each time).
In the end I'll end up by replacing the repeater with a rendered control (for the extra flexibility) and start wondering where all my time has gone.
Requesting Gravatar... Jacky_xu Nov 27, 2007 12:19 AM
# 【收藏】本周ASP.NET英文技术文章推荐[04/08 - 04/14]
要本期共有6篇文章:ASP.NET编译问题的公开Hotfix补丁期待下个版本AjaxPro的发布在ASP.NET2.0中使用MultiView控件实现多页面表单数...
Requesting Gravatar... Mehdi Entezary Mar 26, 2008 5:54 PM
# re: Databinding Tips: Nesting Eval Statements
Many Thanks,
I was trying to call a JavaScript function which would get 3 parameters. This trick did the job. Thanks.

What do you have to say?

(will show your gravatar)
Please add 3 and 2 and type the answer here: