ASP. NET Eval can be used for database binding. However, the specific operations seem complicated. Next we will introduce a method for ASP. NET Eval to help write database binding expressions.
ASP. NET Eval method is TemplateControl, while 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 Eval 1. x era, the general form of data binding expressions is:
- <%# DataBinder.Eval(Container,“DataItem.Name”) %>
In ASP. NET Eval 2.0, the same code can be written as follows:
- <%# Eval(“Name”)%>
How is ASP. NET Eval 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 added in 2.0. It is used to replace Container. DataItem in ASP. NET Eval 1. x.
It seems that we do not understand the GetDataItem () method, and it is difficult to understand the principles of ASP. NET Eval. 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 Eval 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:
- ProtectedVirtualVoidDataBind (BoolRaiseOnDataBinding)
- {
- BoolFlag1 =False;// The use of this sign is easily introduced in the context. If DataItem is used to press the stack, the stack is output later.
- If(This. IsBindingContainer)// Determine whether the control is a data-bound container. In fact, it is to determine whether the control class implements INamingContainer.
- {
- BoolFlag2;
- ObjectObj1 = DataBinder. GetDataItem (This,OutFlag2 );// 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)// Check whether the DataBinding event is triggered.
- {
- This. OnDataBinding (EventArgs. Empty );
- }
- This. DataBindChildren ();// Bind data to the Child control. If the control has 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 to call the Pop method of _ dataBindingContext
- }
- }
- }
Now, we have a full understanding of how the GetDataIten and Eval methods work in ASP. NET Eval 2.0.
Efficiency:
Undoubtedly, the efficiency of Container conversion is the highest, ASP. NET Eval eventually calls DataBinder. eval method, DataBinder. eval uses reflection to obtain data, which is obviously inferior to forced 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.
Finally, ASP. NET Eval or DataBinder. Eval. Both methods use reflection to find 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 Eval 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 Eval 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. ASP. NET Eval expressions are nice-looking. GeDataItem is also a good choice even with so much extreme emphasis on 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.
This article is from blog Author: Anonymous
- Differences between Bind and Eval in ASP. NET: two types of binding
- XML data of ASP. NET
- Development of ASP. NET template controls
- Improvement of ClientID in ASP. NET 4.0
- Analysis on the internal mechanism of ASP. NET data binding