This article assumes that you already know the ASP.net 1.1 data binding (especially container this local variable) mechanism, where the main analysis of ASP 2.0 data bindings customization of those improvements.
The ASP.net 2.0 data binding function eval () simplifies the ASP 1.1 cryptic Container.DataItem, such as data-binding expressions:
<%# (Container.DataItem as DataRowView) ["ProductName"]. ToString ()%>
asp.net 1.1 is simplified as: (removed type designation, Eval is implemented by reflection, this article is no longer elaborated)
<%# DataBinder.Eval (Container.DataItem, "ProductName"). ToString ()%>
ASP.net 2.0 is also simplified to remove the container local variables:
<%# Eval ("ProductName")%>
So how does Page.eval know that "ProductName" is the attribute of that data, that Container.DataItem really disappeared?
Eval () is the TemplateControl method of the page's parent class
Templatecontrol.eval () can automatically compute the container, and the mechanism is obtained from a databindingcontext:stack stack.
1. Establish DataItem Container stack:
In Control.DataBind (), this ensures that the DataItem container of the child control is always on top of the stack.
public class control
{
protected virtual void DataBind (bool raiseondatabinding)
{
BOOL foundDataItem = false;
if (this. Isbindingcontainer)
{
Object o = Databinder.getdataitem (this, out foundDataItem);
if (foundDataItem)
Page.pushdataitemcontext (o); <--the DataItem onto the stack
}
Try
{
if (raiseondatabinding)
OnDataBinding (Eventargs.empty);
Databindchildren (); <--Binding child controls
}
Finally
{
if (foundDataItem)
Page.popdataitemcontext (); <--will dataitem pop up stack
}
}
}
2. Get DataItem Container
public class Page
{
public Object Getdataitem ()
{
...
return This._databindingcontext.peek (); <--reads the DataItem Container at the top of the stack, which is the dataitem that is being bound Container
}
}
3. Templatecontrol.eval ()
public class TemplateControl
{
Protected string Eval (string expression, string format)
{
Return DataBinder.Eval (Page.getdataitem (), expression, format);
}
}
Conclusion:
From the above it can be seen that page.eval () in the calculation or reference Container.DataItem, only this DataItem through the DataItem container stack automatically calculated. I think Page.eval () seems to have simplified the problem, in fact, to make the problem more mysterious.