asp.net eval method for data binding _ Practical Tips

Source: Internet
Author: User

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.

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.