Leisurely self-eval () and databinder. eval () methods in ASP. NET
Eval ("") and bind ("") one-way binding and one-way binding
BIND is a two-way binding, but can be used only when the data source can be changed
ASP. NET 2.0 improves the Data Binding operation in the template. It simplifies the Data Binding syntax databinder. eval (container. dataitem, fieldname) in v1.x to eval (fieldname ). The eval method, like databinder. Eval, can accept an optional formatted string parameter. Shortened eval syntax and databinder. the difference between Eval is that Eval will automatically parse fields based on the dataitem attribute of the most recent container object (such as datalistitem), while databinder. eval needs to use parameters to specify the container. For this reason, Eval can only be used in the template of the data-bound control, but cannot be used in the page layer. Of course, the ASP. NET 2.0 page still supports databinder. Eval. You can use it in environments that do not support simplified eval syntax.
The following example demonstrates how to bind a new simplified eval Data Binding syntax to the image, label, and hyperlink controls in the datalist data item template (itemtemplate.
<Asp: datalist id = "datalist1" repeatcolumns = "5" width = "600" runat = "server" performanceid = "objectperformance1">
<Itemtemplate>
<Asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = & apos; <% # eval ("photoid", "photoformviewplain. aspx? Id = {0} ") %> & apos;>
<Asp: Image id = "image1" runat = "server" imageurl = & apos; <% # eval ("FILENAME", "images/thumbs/{0 }") %> & apos;/> </ASP: hyperlink>
<Asp: Label id = "captionlabel" runat = "server" text = & apos; <% # eval ("caption") %> & apos;/>
</Itemtemplate>
</ASP: datalist> <br/>
<Asp: objectdatasource id = "objectperformance1" runat = "server" typename = "datacomponenttableadapters. photostableadapter" selectmethod = "getphotosforalbum">
Data Binding can also be part of the theme definition of the control, so that we can freely change the layout and appearance of the templated control by changing the theme. However, the theme template can only use eval (or bind discussed later ). Binding to any user code is forbidden.
Q:What is the difference between databinder. eval (container. dataitem, "name") and container. dataitem ("name?
A: databinder is system. A static class in the Web, which provides the eval method to simplify writing data binding expressions. However, it uses reflection and other overhead methods to achieve ease of use, therefore, its performance is not the best. The container is not a static object or method at all. It is ASP. NET page compiler local variables declared in the Data Binding event handler, its type is the data container type of controls that can be bound to data (for example, the data binding container inside the repeater is called repeateritem). In these container classes, dataitem attributes are basically available, therefore, you can write container. dataitem, which returns the data item from the data source you are bound. If your data source is datatable, the Data Type of this data item is actually datarowview.
Q: source code of the eval () method? What are the performance differences between databinder. eval () and eval?
A: Actually, 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:
<% # Eval ("name") %>
How is ASP. NET 2.0 implemented? First, we will study the eval method. By reflecting the source code of the. NET Framework 2.0 class library, we can see that this method is implemented as follows:
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 newly added method in 2.0. It is used to replace container. dataitem in ASP. NET 1. X.
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
}
}
}
Now, we have a full understanding of the working principles of getdataiten and eval methods in ASP. NET 2.0.
Common binding formats, but their performance varies.
<% # Databinder. eval (container. dataitem, "[N]") %>
<% # Databinder. eval (container. dataitem, "columnname") %>
<% # Databinder. eval (container. dataitem, "columnname", null) %>
<% # Databinder. eval (container, "dataitem. columnname", null) %>
<% # (Datarowview) container. dataitem) ["columnname"] %>
<% # (Datarowview) container. dataitem). Row ["columnname"] %>
<% # (Datarowview) container. dataitem) ["adtitle"] %>
<% # (Datarowview) container. dataitem) [N] %>
<% # (Dbdatarecord) container. dataitem) [0] %>
<% # (Custom type) container. dataitem). Attribute. tostring () %> (if the attribute is of the string type, tostring () is not used)
The above three have the best performance.