Tuesday, August 10, 2010

Using EVAL in asp.net

http://www.15seconds.com/issue/040630.htm


In ASP.NET 2.0, you should generally use the Eval method. And, to make this easier, the DataBinder object that exposes it has been made the default context for all non-hierarchical (rows and columns) data binding expressions. This means that you can use a much simpler syntax. If you just want to display a value without formatting it, use the Eval method directly:


<%# Eval("expression") %>

Or, if you need to format the value, use the second overload that accepts a format string:

<%# Eval("expression"[, "format"]) %>

The format parameter works here just like it does in v1.x. So, for example, you can format the value in a column named Price as currency using:

<%# Eval("Price", "{0:C}") %>

And, as in v1.x, you can use more complex format strings to display text as well as the current row value:

<%# Eval("Price", "Special Offer {0:C} for Today Only!") %>

No comments:

Post a Comment