Explanation of the Eval method in ASP. NET 2.0

Source: Internet
Author: User

In fact, the Eval method is TemplateControl, while the System. web. UI. page and System. web. UI. userControl inherits from TemplateControl, so we can directly call a method on Page and UserControl.

The Page. Eval method can help us better write data binding expressions. In the ASP. NET 1. x era, the general form of data binding expressions is:
 

<% # DataBinder. Eval (Container, "DataItem. Name") %> in ASP. NET 2.0, the same Code can be written as follows: Code
<% # Eval ("Name") %>
How is ASP. NET 2.0 implemented? We will first study the Eval method. By reflecting the source code of the. NET fromwork 2.0 class library, we can see that this method is implemented in this way: Protected internal object Eval (string expression)
{
This. CheckPageExists ();
Return DataBinder. Eval (this. Page. GetDataItem (), expression );
} We don't have to worry about the first line. This is to check whether there is a Page object during the call. If not, an exception will be thrown. The key is the second line: Return DataBinder. eval (this. page. getDataItem (), expression); Page. getDataItem () is also a new method in 2.0. It is used to replace ASP. NET 1. container in x. dataItem.
It seems that the GetDataItem () method is hard to understand the Eval principle. The implementation of GetDataItem is also very 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 is an internal object _ dataBindingContext, which is a Stack type by checking the source code. So he has the Peek method. This piece of code is easy to understand. First, judge whether the Stack is instantiated, and then determine whether there are any elements in the Stack. If the Stack is not instantiated or has no elements, an exception is thrown. Finally, return the elements at the top of the stack.
ASP. NET 2.0 uses a Stack to save the so-called DataItem. We soon found the method for compressing and popping the element for this Stack: Control. DataBind: Protected virtual void DataBind (bool raiseOnDataBinding)
{
Bool flag1 = false; // The use of this flag is easily introduced in the context. If DataItem is used to press the stack, the stack is output later.
If (this. IsBindingContainer) // determines whether the control is a data-bound container. In fact, it is used to determine whether the control class implements INamingContainer.
{
Bool flag2;
Object obj1 = DataBinder. GetDataItem (this, out flag2); // this method is used to determine whether the control has the DataItem attribute and obtain it.
If (flag2 & (this. Page! = Null) // if the control has DataItem
{
This. Page. PushDataBindingContext (obj1); // press DataItem to stack. PushDataBindingContext is to call the Push method of _ dataBindingContext.
Flag1 = true;
}
}
Try
{
If (raiseOnDataBinding) // you can check whether the DataBinding event is triggered.
{
This. OnDataBinding (EventArgs. Empty );
}
This. dataBindChildren (); // bind data to the Child control. If the control has a DataItem, The DataItem is pushed to the top of the stack. In this way, the Eval or GetDataItem method is called in the Child control, the DataItem you just pressed will be taken out.
}
Finally
{
If (flag1) // if there is a stack, it will pop up now.
{
This. Page. PopDataBindingContext (); // PopDataBindingContext is the Pop method that calls _ dataBindingContext
}
}
}


So far, we have a full understanding of the working principles of the GetDataIten and Eval methods in ASP. NET 2.0. Next time I plan to study the new Bind syntax in ASP. NET 2.0.

If you provide Bind syntax materials and make good suggestions, you can give scores as appropriate. There are very few groups such as up and top, and no score is given.

Efficiency:Undoubtedly, the efficiency of Container conversion is the highest. Eval eventually calls the DataBinder. Eval method, and DataBinder. Eval uses reflection to obtain data, which is obviously not as efficient as data conversion. We can compare various methods:
(Type) Container. DataItem). Property
This method is the most efficient, because there is no reflection.

The second is: (Type) GetDataItem (). Property
The reason for the low efficiency of this method is that there is one more Stack of Peek operations. Of course, in fact, this difference can be ignored.

Last, Eval or DataBinder. Eval. Both methods use reflection to search for attributes or indexer members, reducing the efficiency.

Another noteworthy problem is that all controls that implement the INamingContainer interface should implement the IDataItemContainer interface, because in the Control. if you find that the control implements the INamingContainer interface during DataBind, you will try to find its DataItem. If the control does not implement IDataItemContainer, DataBinder. the GetDataItem method uses reflection to check whether the control has an attribute member called DataItem. Obviously, this is not what we want to see. In fact, ASP. NET also has a tag interface: INonBindingContainer. controls that implement the INamingContainer interface can choose to implement this command at the same time to run ASP. NET does not look for DataItem, but unfortunately, I do not know what Microsoft is for, this interface is internal ......

In fact, you don't need to pay too much attention to efficiency. The Eval expression looks good. Even if you have so much attention to efficiency, GeDataItem is also a good choice. Undoubtedly, the efficiency of Container conversion is the highest. Eval eventually calls the DataBinder. Eval method, and DataBinder. Eval uses reflection to obtain data, which is obviously not as efficient as data conversion.
Address: http://blog.sdtv.com.cn/user1/11/1815.html

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.