Ryan Mick posted on March 04, 2009 16:04

I don't know why I never though of this before, I guess I never really looked that far into it. But the other day I discovered that you can nest Eval calls for databinding. This came about when we had a need to be able to dynamically change the format of the data in a cell of a gridview. Our solution was to have the query return a column with the .Net format string to be used for the value. We then converted the bound column into a template column and added a second Eval call to the format column as the second parameter like this:
Eval("Value", Eval("Format"))
And there you have it. Dynamic formating by using nested Eval calls.
This also made me look into a irritating problem I was having with the NavigateURL property of a Hyperlink control. I wanted to databind the row id to the end but it always gave me an error
NavigateURL='~/somepage.aspx?id=' + '<%# Eval("ID") %>'
I even tried other formats like:
NavigateURL='~/somepage.aspx?id=<%# Eval("ID") %>'
But none of it worked, so I realized that I was going about it the wrong way. The way to do this is like this:
NavigateURL='<%# "~/somepage.aspx?id=" + Eval("ID") %>'
That works like a charm. I should have figured it out years ago but honestly I never really cared enough to look into it. It was just as easy to bind it in the code behind and be done with it. Hopefully that will point someone else in the write direction as well.