The mechanism of the ASP.NET2.0 data binding function eval ()

Source: Internet
Author: User
Tags bool eval expression string format tostring

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.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.