The eval method for revealing ASP.net 2.0

Source: Internet
Author: User
Tags bind bool eval expression
asp.net

In fact, the Eval method is TemplateControl, and System.Web.UI.Page and System.Web.UI.UserControl all inherit from TemplateControl, so we can call a method directly on page and UserControl.

The Page.eval method can help us write data-binding expressions better, in the ASP.net 1.x era, the general form of data-binding expressions is:

<%# DataBinder.Eval (Container, "Dataitem.name")%>

And in ASP.net 2.0, the same code, we can write this:

<%# Eval ("Name")%>

asp.net 2.0 How to achieve it? We'll start with the Eval method, which we can see by reflecting the source code of the. NET Framework 2.0 class libraries:

Protected internal Object Eval (string expression)

{

This. Checkpageexists ();

Return DataBinder.Eval (this. Page.getdataitem (), expression);

}

The first line we do not have to, this is to check the call when there is no Page object, if not it will throw an exception.

The key is the second line:

Return DataBinder.Eval (this. Page.getdataitem (), expression);

Page.getdataitem () is also a new method in 2.0, which replaces the Container.DataItem in ASP.net 1.x.

It seems difficult to understand the principle of eval without knowing the Getdataitem () method. The implementation of Getdataitem is also simple:

public Object Getdataitem ()

{

if ((This._databindingcontext = null) | | (this._databindingcontext.count = 0))

{

throw new InvalidOperationException (SR. GetString ("Page_missingdatabindingcontext"));

}

return This._databindingcontext.peek ();

}

We noticed that there was an internal object _databindingcontext that was found to be a stack type by looking through the source code. So he has a peek method. This piece of code is easy to understand, first to determine whether the stack is instantiated, and then, to determine if the stack has any elements, if the stack is not instantiated or no element throws an exception. Finally, the elements at the top of this stack are returned.

ASP.net 2.0 uses a stack to preserve the so-called dataitem, and we quickly find a way to press and eject elements for this stack: the Control.DataBind method:

protected virtual void DataBind (bool raiseondatabinding)

{

BOOL Flag1 = false;//The use of this flag is easy to launch in the context, if there is dataitem pressure stack, then the stack in the back.

if (this. Isbindingcontainer)//To determine whether the control is a data binding container, is actually to determine whether the control class is implemented INamingContainer

{

BOOL Flag2;

Object obj1 = Databinder.getdataitem (this, out flag2);//This method is to determine whether the control has a DataItem attribute and take it out.

If Flag2 && (this. Page!= null)//If the control has DataItem

{

This. Page.pushdatabindingcontext (OBJ1)//Put DataItem pressure stack, Pushdatabindingcontext is to call _databindingcontext push method

Flag1 = true;

}

}

Try

{

if (raiseondatabinding)//Here is the judge that is not triggering the DataBinding event.

{

This. OnDataBinding (Eventargs.empty);

}

This. Databindchildren ();//The child control is data bound, and if the control has a DataItem, the DataItem is pressed onto the top of the stack so that the eval or Getdataitem method is invoked inside the control. You'll get the dataitem you just pressed in.

}

Finally

{

if (FLAG1)//If there is a pressure stack, now bounce out.

{

This. Page.popdatabindingcontext ();//popdatabindingcontext is the pop method that calls _databindingcontext

}

}

}

Now that we have a complete understanding of how the Getdataiten and eval methods work in ASP.net 2.0, the next time I intend to study the new bind syntax in asp.net 2.0.

has provided the bind grammar material and proposes the good suggestion the discretion to give the cent, up, the top and so on the branch very few, receives the cent no cent.



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.