Differences between asp.net bind () and eval ()

Source: Internet
Author: User
Tags bind eval reflection string format

Eval () method
During runtime, reflection is used to perform post-binding computation. Therefore, compared with the standard ASP. NET data binding method bind, performance will be significantly reduced. It is generally used to format strings when binding. Use this method as few as possible in most cases
The Eval method is a static (read-only) method that uses the value of a data field as a parameter and returns it as a string. The Bind method supports the read/write function. You can retrieve the value of the data-bound control and submit any changes to the database.
Use the Eval method
The Eval method can calculate the post-bound data expression in the template of a data binding control (such as the GridView, DetailsView, and FormView controls. At runtime, the Eval method calls the Eval method of the DataBinder object and references the current data item of the named container. A naming container is usually the smallest component of a data binding control that contains a complete record, such as a row in the GridView control. Therefore, the Eval method can only be used for binding to the template of the data binding control.
The Eval method uses the name of a data field as a parameter and returns a string containing the value of this field from the current record of the data source. The second parameter can be provided to specify the format of the returned string. This parameter is optional. String Format parameters are defined using the Format method of the String class.

Eval () 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.

Eval () binding method

1. Data binding syntax in 1.x

The code is as follows: Copy code

<Asp: Literal id = "liteval" runat = "server" Text = '<% # DataBinder. Eval (Container. DataItem, "userName") %>'/>

2. 2.x simplified Eval data binding syntax

The code is as follows: Copy code

<Asp: Literal id = "litEval1" runat = "server" Text = '<% Eval ("userName") %>'/>

3. Method overload of Method 2

The code is as follows: Copy code

<A href = '<% # Eval ("userId", "Default. aspx? Id = {0} ") %> '> <% # Eval (" userName ") %> </a>

4. Bind two eval values at the same time.

The code is as follows: Copy code

<A href = '<% # string. Format ("Default. aspx? Id = {0} & role = {1} ", Eval (" userId "), Eval (" userRole ") %> '> <% # Eval (" userName ") %> </a>

Bind () method

The Bind method has some similarities with the Eval method, but there are also great differences. Although the Bind method can be used to retrieve the value of a data binding field like the Eval method, the Bind method must be used when the data can be modified.

In ASP. NET, data binding controls (such as the GridView, DetailsView, and FormView controls) can automatically use the update, delete, and insert operations of data source controls. For example, if you have defined SQL Select, Insert, Delete, and Update statements for the data source control, you can use the Bind method in the GridView, DetailsView, or FormView control template, the control can extract values from the child control in the template and pass these values to the data source control. The data source control then executes appropriate database commands. For this reason, Bind functions must be used in EditItemTemplate or InsertItemTemplate of the data binding control.

The Bind method is usually used with the input control, for example, the TextBox control rendered by the row of the GridView in editing mode. When the data binding control creates these input controls as a part of its own rendering, this method can extract the input value.

The Bind method uses the name of the data field as the parameter to associate with the binding property, as shown in the following example.


The Bind method is usually used with the input control, for example, the TextBox control rendered by the row of the GridView in editing mode. When the data binding control creates these input controls as a part of its own rendering, this method can extract the input value.

The Bind method uses the name of the data field as the parameter to associate with the binding property, as shown in the following example:

The code is as follows: Copy code

<EditItemTemplate>
<Table>
<Tr>
<Td align = right>
<B> Employee ID: </B>
</Td>
<Td>
<% # Eval_r ("EmployeeID") %>
</Td>
</Tr>
<Tr>
<Td align = right>
<B> First Name: </B>
</Td>
<Td>
<Asp: TextBox ID = "EditFirstNameTextBox" RunAt = "Server"
Text = '<% # Bind ("FirstName") %>'/>
</Td>
</Tr>
<Tr>
<Td align = right>
<B> Last Name: </B>
</Td>
<Td>
<Asp: TextBox ID = "EditLastNameTextBox" RunAt = "Server"
Text = '<% # Bind ("LastName") %>'/>
</Td>
</Tr>
<Tr>
<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 of a row, the attribute values of each control bound with the Bind syntax are extracted and passed to the data source control for Update.


Use DataBinder. Eval
ASP. NET provides a static method named DataBinder. Eval. This method computes the bound data binding expression and formats the result as a string (optional ). This method avoids many explicit forced conversions that must be performed when the value is forced to the desired data type.

For example, in the following code snippet, an integer is displayed as a currency string. To use the standard ASP. NET data binding syntax, you must first forcibly convert the data row type to retrieve the data field IntegerValue. Then, this will be passed to the String. Format method as a parameter:

The code is as follows: Copy code

<% # String. Format ("{0: c}", (DataRowView) Container. DataItem) ["IntegerValue"]) %>


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

The code is as follows: Copy code

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

The code is as follows: Copy code
<% # (Bool) DataBinder. eval_r (Container. DataItem, "BoolValue") %>

DataBinder. Eval is particularly useful when you bind data to controls in the templated list, because both data rows and data fields must be forcibly converted.


In the TemplateField template, Bind and Eval methods can be used to restrict or retrieve the values of a column in the database. The following is the format of the Bind method, and the Eval format is the same as that of the Bind method. Bind ("column name", "displayed format ")

For example, we want to obtain a date type data. In the database, the column name is updated and the value is 2008/06/01. However, if you want to display this information on April 9, June 01, 2008, you can write Bind ("updated", "{0: MM dd, yyyy}") and Eval.

Both can read and display the values in the data. When we use the edit update operation, Bind can automatically update the modified value to the database and display the modified value. However, when Eval is used, only the error screen is displayed. 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 both read and update data, but the Eval method can only read the display data. Therefore, when we select Bind and Eval methods, we must compete. When data must be updated, we should use Bind to only display data, you can use the Eval method without any operations.


Difference between bind () and eval ()
1. Eval is read-only data and Bind is updatable.
2. When performing the next expression operation, you must use Eval such as <% # Eval ("field name"). ToString (). Trim () %>
3. If the content format DataFormateString = {0: d} is set in the bind column in The GridView, the HtmlCode attribute must be set to false; otherwise, the attribute cannot work;
Eval one-way binding: data is read-only
Bind two-way binding: The data can be changed and returned to the server. The server can process the changed data, such as storing the data in the database.

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.