Let's say you already know the mechanics of asp.net eval 1.1 data binding (especially container this local variable), and here's a major analysis of ASP.net eval 2.0 data bindings for those improvements.
The ASP.net eval 2.0 data-binding function eval () simplifies the ASP.net eval 1.1 mysterious Container.DataItem, such as data-binding expressions:
Copy Code code as follows:
<%# (Container.DataItem as DataRowView) ["ProductName"]. ToString ()%>
asp.net eval 1.1 is simplified as follows: (the type designation is removed, Eval is implemented by reflection, this article is no longer elaborated)
Copy Code code as follows:
<%# DataBinder.Eval (Container.DataItem, "ProductName"). ToString ()%>
asp.net Eval 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?
asp.net Eval () is the parent class TemplateControl method of the page
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.
Copy Code code as follows:
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
Copy Code code as follows:
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 ()
Copy Code code as follows:
public class TemplateControl
{
Protected string Eval (string expression, string format)
{
Return DataBinder.Eval (Page.getdataitem (), expression, format);
}
}
Conclusion:
From the above see Page.eval () in the calculation or reference the Container.DataItem, but this DataItem through the DataItem container stack automatically calculated. I think Page.eval () seems to have simplified the problem. , actually make the problem more mysterious.