Differences between bind and eval

Source: Internet
Author: User
1. Briefly describe the differences between eval and bind

Binding expression
<% # Eval ("field name") %>
<% # BIND ("field name") %>

Difference 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 () %>

2. 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 it in the database.

2. describe in detail the eval () method to run post-bound Computing Using Reflection. Therefore, compared with the standard ASP. NET data binding method bind, the 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.

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:

< Edititemtemplate >
< Table >
< Tr >
< TD Align = Right >
< B > Employee ID: </ B >
</ TD >
< TD >
<% # Eval ("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 exampleCodeIn the segment, 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:

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

<%# Databinder. eval (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:

<%# (Bool) databinder. eval (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.

3. other Eval: displays read-only data. BIND: You can bind read-only data or update data. The bind method also associates fields with the binding property of the control, this makes it possible to use the update, insert, and delete methods of data controls (such as the gridview) for corresponding processing.

The data bound to eval and bind is displayed in <% # %>. If you modify or operate the data to be displayed, it is also in <% # %>. For example:

1. display the displayed strings as characters: <% # (eval ("Address"). tostring (). substring () %>. display the 10-digit address.

2. Determine the displayed information: <% # (eval ("if_delete"). tostring = "yes "? "Deleted": "Not deleted" %>

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.