function | Data This article assumes that you already know the mechanism of ASP 1.1 data binding (especially container this local variable), where the main analysis of ASP 2.0 data bindings made 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 1.1 is simplified to: (Remove the type designation, Eval is implemented by reflection, this article is no longer elaborated)
<%# DataBinder.Eval (Container.DataItem, "ProductName"). ToString ()%>
ASP 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 ()
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.