Example: ASP. NET Eval: How to Write database binding expressions

Source: Internet
Author: User

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:

 
 
  1. <%# DataBinder.Eval(Container,“DataItem.Name”)   %> 

In ASP. NET Eval 2.0, the same code can be written as follows:

 
 
  1. <%# 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:

 
 
  1. protected   internal   object   Eval(string   expression)     
  2. {     
  3.      this.CheckPageExists();     
  4.      return   DataBinder.Eval(this.Page.GetDataItem(),   expression);     
  5. }  

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:

 
 
  1. 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:

 
 
  1. public   object   GetDataItem()     
  2. {     
  3.      if   ((this._dataBindingContext ==  null) || (this._dataBindingContext.Count == 0))     
  4.      {     
  5.            throw   new   InvalidOperationException(SR.GetString("Page_MissingDataBindingContext"));     
  6.      }     
  7.      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:

 
 
  1. ProtectedVirtualVoidDataBind (BoolRaiseOnDataBinding)
  2. {
  3. 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. 
  4. If(This. IsBindingContainer)// Determine whether the control is a data-bound container. In fact, it is to determine whether the control class implements INamingContainer. 
  5. {
  6. BoolFlag2;
  7. ObjectObj1 = DataBinder. GetDataItem (This,OutFlag2 );// This method is used to determine whether the control has the DataItem attribute and obtain it. 
  8. If(Flag2 &&(This. Page! =Null))// If the control has DataItem 
  9. {
  10. This. Page. PushDataBindingContext (obj1 );// Press DataItem to stack. PushDataBindingContext is to call the Push method of _ dataBindingContext. 
  11. Flag1 =True;
  12. }
  13. }
  14. Try
  15. {
  16. If(RaiseOnDataBinding)// Check whether the DataBinding event is triggered. 
  17. {
  18. This. OnDataBinding (EventArgs. Empty );
  19. }
  20. 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. 
  21. }
  22. Finally
  23. {
  24. If(Flag1)// If there is a stack, it will pop up now. 
  25. {
  26. This. Page. PopDataBindingContext ();// PopDataBindingContext is to call the Pop method of _ dataBindingContext 
  27. }
  28. }
  29. }

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:

 
 
  1. ((Type)   Container.DataItem).Property  

This method is the most efficient, because there is no reflection.

The second is:

 
 
  1. ((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

  1. Differences between Bind and Eval in ASP. NET: two types of binding
  2. XML data of ASP. NET
  3. Development of ASP. NET template controls
  4. Improvement of ClientID in ASP. NET 4.0
  5. Analysis on the internal mechanism of ASP. NET data binding

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.