Eval: Sometimes I like spagheti code
I just discovered some interesting features of the construct <%# Eval() %>. Eval function uses reflection to evaluate a property of a bound object.
I have two classes Product and Category and I want to display the available products in a page, by using a data bound control.
In a data template ( ItemTemplate for sample ) I can use the Eval function like:
1. very simple evaluation without using formats
2. using a format
3. Combining two or more properties:
notice that the operators "+" are in the construction <%# %> and the expression is not a concatenation between different constructs
4. Can use more dots!
5. Can use casting
6. What about if you need to compare things
7. Are you aware of a null exception ?
All can be rewritten in the code behind, on ItemDataBound event. Sometimes I'm too lazy or bored to add a handler and I prefer to write such kind of Eval expressions. The disadvantages can be the heavy use of reflection or the spaghetti code. A plus is you don't need recompile the things as you would be needed when you treat it on ItemDataBound event.
I have two classes Product and Category and I want to display the available products in a page, by using a data bound control.
public class Product {
public int ID;
public string Name;
public int Stock;
public DateTime Date;
public Category Category;
}
public class Category {
public string Name;
public DateTime Date;
}
In a data template ( ItemTemplate for sample ) I can use the Eval function like:
1. very simple evaluation without using formats
<%# Eval("Name") %>
- will output the name<%# Eval("Stock") %>
- will output the stock ( ..and so on )2. using a format
<%# Eval("Date","{0:yyyy MMM dd}") %>
- for sample: 2009 Apr 06<%# Eval("Stock","{0} items") %>
- will output "10 items" if the stock is 103. Combining two or more properties:
<%# "The " + Eval("Name") + " has " + Eval("Stock") + " items in stock." %>
notice that the operators "+" are in the construction <%# %> and the expression is not a concatenation between different constructs
4. Can use more dots!
<%# Eval("Category.Name") %>
5. Can use casting
<%# (int)Eval("Stock") %>
6. What about if you need to compare things
<%# ((int)Eval("Stock") >= 1) ? ("<a href='products.aspx?id=" + Eval("ID") + "'>Buy now!!!</a>") : "Not in stock!" %>
7. Are you aware of a null exception ?
<%# (Eval("Category") != null) ? Eval("Category.Name") : "No category" %>
All can be rewritten in the code behind, on ItemDataBound event. Sometimes I'm too lazy or bored to add a handler and I prefer to write such kind of Eval expressions. The disadvantages can be the heavy use of reflection or the spaghetti code. A plus is you don't need recompile the things as you would be needed when you treat it on ItemDataBound event.
In addition you can use code-behind code (methods) to avoid long Eval expressions or add extra processing to the data, like this:
ReplyDelete<%# myFunction(Eval("data") %>
please note that the function must be marked as protected in the code-behind so that the databinder can call.
protected string myFunction(object arg)
{
//extra processing here
}