The difference between ASP () and eval ()

Source: Internet
Author: User

Eval () method
Late-bound calculations are performed using reflection at run time, so performance is significantly reduced compared to the standard ASP. NET data binding method. It is typically used when binding requires formatting a string. Use this method sparingly in most cases
The Eval method is a static (read-only) method that takes the value of a data field as a parameter and returns it as a string. The Bind method supports read/write functionality that retrieves the value of a data-bound control and submits any changes back to the database.
Using the Eval method
The Eval method calculates a late-bound data expression in a template for data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method invokes the Eval method of the DataBinder object, referencing the current data item of the naming container. A naming container is typically the smallest part of a data-bound control that contains a complete record, such as a row in a GridView control. Therefore, you can only use the Eval method on bindings within the template of a data-bound control.
The Eval method returns a string containing the value of the field from the current record of the data source with the name of the data field as an argument. You can provide a second argument that specifies the format of the returned string, which is an optional parameter. The string format parameter uses the syntax defined for the format method of the string class.

About Eval () efficiency

It is doubtful that the efficiency of the strongly-typed conversion container is the highest, and that Eval is ultimately called the DataBinder.Eval method, and DataBinder.Eval is using reflection to get the data, which is obviously not as strong as a strongly typed data conversion.

     We can compare various methods:    
    
((Type)    Container.DataItem). property  
 
     This method is the most efficient because there is no reflection.    
    
     followed by:    
 
(Type)    Getdataitem ()). property  
 
     This method is inefficient because there is more than one stack of peek operations, of course, in fact craved differences can be ignored.    
    
     finally:    Eval or DataBinder.Eval, both of these methods use reflection to find properties or indexer members, which is inefficient.    
    
     Another notable problem is that All control that implements the INamingContainer interface should implement the Idataitemcontainer interface, Because in Control.DataBind, if the control implements the INamingContainer interface, it will try to find its DataItem, if the control does not implement Idataitemcontainer, then Databinder.getdataitem The method uses reflection to see if the control has a property member called DataItem, which is obviously not what we want to see.  

In fact, ASP. NET also has a markup interface: Inonbindingcontainer, the implementation of the INamingContainer interface control can choose to implement this to command ASP. NET not to look for DataItem, but unfortunately, do not know Microsoft for what purpose, this interface is in The ternal ...

In fact, there is no need to pay too much attention to the efficiency, eval expression is very good, even if there is so extreme emphasis on efficiency, Gedataitem is also a good choice. There is no doubt that the high efficiency of the strongly typed conversion container is the highest, and Eval is ultimately called the DataBinder.Eval method, and DataBinder.Eval uses reflection to get the data, which is obviously less than the strongly typed data conversion.

Eval () Binding method

1. Data binding syntax in 1.x

Code to copy code as follows
<asp:literal id= "LitEval2" runat= "server" text= ' <% #DataBinder. Eval (Container.DataItem, "UserName")%> '/ >

2.2.x simplified eval data binding syntax

Code to copy code as follows
<asp:literal id= "http://www.hzhuti.com/nokia/5900xm/" runat= "server" text= ' <%eval ("UserName")%> '/>

3. Method overloading of the second method

Code to copy code as follows
<a href= ' <%# eval ("UserId", "default.aspx?id={0}")%> ' ><%# eval ("UserName")%></a>

4. Eval binds two values at a time

Code to copy code as follows
<a href= ' <%# string. Format ("Default.aspx?id={0}&role={1}", eval ("UserId"), eval ("Userrole"))%> ' ><%# eval ("UserName")% ></a>

Bind () method

There are some similarities between the Bind method and the Eval method, but there are also significant differences. Although you can use the Bind method to retrieve the value of a data-bound field as you would with the Eval method, use the Bind method when the data can be modified.

Data-bound controls, such as the GridView, DetailsView, and FormView controls, automatically use the update, delete, and insert operations of the data source control in ASP. For example, if you have defined SQL Select, Insert, Delete, and Update statements for a data source control, you can make the control from a child control in the template by using the Bind method in the GridView, DetailsView, or FormView control template Extracts the values and passes them to the data source control. The data source control then executes the appropriate database commands. For this reason, the bind function is used in the EditItemTemplate or insertitemtemplate of a data-bound control.

The Bind method is typically used with an input control, such as a TextBox control rendered by a GridView row in edit mode. When data-bound controls create these input controls as part of their own rendering, the method can extract the input values.

The Bind method takes the name of the data field as an argument and is associated with the binding property, as shown in the following example


The Bind method is typically used with an input control, such as a TextBox control rendered by a GridView row in edit mode. When data-bound controls create these input controls as part of their own rendering, the method can extract the input values.

The Bind method takes the name of the data field as an argument and is associated with the binding property, as shown in the following example:

Code to copy code as follows
<EditItemTemplate>
<table>
<tr>
&LT;TD align=right>
<b>employee id:</b>
</td>
<td>
<%# eval_r ("EmployeeID")%>
</td>
</tr>
<tr>
&LT;TD align=right>
<b>first name:</b>
</td>
<td>
<asp:textbox id= "Editfirstnametextbox" runat= "Server"
Text= ' <%# Bind ("FirstName")%> '/>
</td>
</tr>
<tr>
&LT;TD align=right>
<b>last name:</b>
</td>
<td>
<asp:textbox id= "Editlastnametextbox" runat= "Server"
Text= ' <%# Bind ("LastName")%> '/>
</td>
</tr>
<tr>
&LT;TD colspan= "2" >
<asp:linkbutton id= "UpdateButton" runat= "Server"
text= "Update" commandname= "Update"/>

<asp:linkbutton id= "Cancelupdatebutton" runat= "Server"
Text= "Cancel" commandname= "Cancel"/>
</td>
</tr>
</table>
</EditItemTemplate>

When you click the Update button for a row, each control property value that is bound with the bind syntax is extracted and passed to the data source control to perform the update operation.


Using DataBinder.Eval
ASP. NET provides a static method named DataBinder.Eval that computes a late-bound data-binding expression and formats the result as a string (optional). With this approach, you can avoid many explicit casts that you must perform when you force a value to the desired data type.

For example, in the following code fragment, an integer is displayed as a currency string. Using the standard ASP. NET data binding syntax, you must first cast the data row type to retrieve the data field IntegerValue. This is then passed as a parameter to the String.Format method:

Code to copy code as follows
<%# String.Format ("{0:c}", ((DataRowView) container.dataitem) ["IntegerValue"])%>


Compare this syntax with DataBinder.Eval syntax, which has only three parameters: the naming container for the data item, the data field name, and the format string. In a templated list (such as the DataList class, the DataGrid class, or the Repeater Class), the naming container is always container.dataitem.

Code to copy code as follows
<%# Databinder.eval_r (Container.DataItem, "IntegerValue", "{0:c}")%>


The format string parameter is optional. If it is ignored, DataBinder.Eval returns the value of the type object, as shown in the following example:

Code to copy code as follows
<%# (BOOL) databinder.eval_r (Container.DataItem, "Boolvalue")%>

DataBinder.Eval is particularly useful when data binding is performed on controls in a templated list, because data rows and data fields are usually forced to be cast.


The bind and Eval methods can be used in the TemplateField template in order to restrict or remove values from a column in the database. The following is the format of the Bind method, and the format of Eval is the same as bind. Bind ("Name of column", "Formatted text displayed")

For example, we want to take a date type of data, in the database column name is updated, the value is 2008/06/01. But like June 01, 2008, we can write bind ("updated", "{0:yyyy mm month DD Day}"), and Eval is the same.

Both can read the values in the data and display them. When we use the edit update operation, bind can automatically update the modified value to the database and display the modified value. But with Eval you can only get the wrong picture, and the new data is not updated to the database.

From this point of view, the difference between the bind method and the Eval method is that the Bind method can read and update data in 2 ways, but the Eval method can only read the displayed data. Therefore, when we choose the Bind method and the Eval method, we must have the contention, when the data must need to update the operation when we should use BIND, just display the data, there is no action can use the Eval method.


Bind () and eval () differences
1, eval is read-only data, bind is updatable.
2, when the operation of the sub-expression, you must use eval such as <%# eval ("Field name"). ToString (). Trim ()%>
3, if the GridView in the binding column inside the content format dataformatestring ={0:d}, you must set the property Htmlcode to False, otherwise it will not work;
Eval One-way binding: Data is read-only
Bind bidirectional binding: Data can be changed and returned to the server side, the server can process the changed data, such as the database

The difference between ASP () and eval ()

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.