To solve your problem, it is important to understand how it Evalworks under the hood.
The statement is Evalconverted to a method call DataBinder.Evalat the aspx compilation stage. This means that the expression <%# Eval("A") %>is an abridged version <%# DataBinder.Eval(Container.DataItem, "A") #>. If you follow the method execution path Eval, you will see that the exception is thrown in GetPropertyValueif the container object does not contain the property:
PropertyDescriptor pd = GetPropertiesFromCache(container).Find(propName, true);
if (pd != null) {
prop = pd.GetValue(container);
}
else {
throw new HttpException(SR.GetString(SR.DataBinder_Prop_Not_Found, container.GetType().FullName, propName));
}
, Eval, SafeEval :
public abstract class BasePage: Page
{
public object SafeEval(object container, string expression)
{
try
{
return DataBinder.Eval(container, expression);
}
catch (HttpException e)
{
Trace.Write("DataBinding", "Failed to process the Eval expression", e);
}
return "Put here whatever default value you want";
}
}
:
<asp:Label runat="server" Text='<%# SafeEval(Container.DataItem, "A") %>'></asp:Label>
, , , /. , ASP.NET Eval.