Differences between eval bind databinder. Eval

Source: Internet
Author: User

Eval, Bind, databinder. Eval

The eval () method uses reflection to execute post-eval () method at runtime to execute post-bound Computing Using Reflection. compared with the bind method of net data binding, 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
. It is generally used to format strings when binding.

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.
Compared with bind, bind causes a significant reduction in performance.
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 reflection to execute post-execution
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.

Use the bind method. It is generally used to format strings when binding.
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. . Use this method as few as possible in most cases

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.
Compared with bind, bind causes a significant reduction in performance.
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> bind computing, so it is related to standard ASP. NET data.
<Tr>
<TD align = right>
<B> employee ID: </B>. Use this method as few as possible in most cases
</TD>
<TD>
<% # Eval ("employeeid") %>. Use this method as few as possible in most cases
</TD>
</Tr>
<Tr>. Use this method as few as possible in most cases
<TD align = right>
<B> First name: </B>
</TD> compared to bind, the binding method causes a significant reduction in performance.
<TD>
<Asp: textbox id = "editfirstnametextbox" runat = "server"
TEXT = '<% # BIND ("firstname") %>'/> is bound to the computing, so it is consistent with the standard ASP. NET data.
</TD>
</Tr>
<Tr>. It is generally used to format strings when binding.
<TD align = right>
<B> last name: </B>
</TD> the eval () method uses reflection to execute post-execution
<TD>
<Asp: textbox id = "editlastnametextbox" runat = "server"
TEXT = '<% # BIND ("lastname") %>'/> the eval () method uses reflection to execute post-execution
</TD>
</Tr>
<Tr> compared to bind, the binding method causes a significant reduction in performance.
<TD colspan = "2">
<Asp: linkbutton id = "updatebutton" runat = "server"
The text = "Update" commandname = "Update"/> eval () method uses reflection to execute later stages during running.

<Asp: linkbutton id = "cancelupdatebutton" runat = "server"
TEXT = "cancel" commandname = "cancel"/>. It is generally used to format strings when binding.
</TD>
</Tr>
</Table> compared to bind, the binding method causes a significant reduction in performance.
</Edititemtemplate>

Therefore, it is bound with standard ASP. NET data.
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.

The eval () method uses reflection to execute post-execution
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.
. It is generally used to format strings when binding.
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:

Compared with bind, bind causes a significant reduction in performance.
<% # String. Format ("{0: c}", (datarowview) container. dataitem) ["integervalue"]) %>

. It is generally used to format strings when binding.
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 eval () method uses reflection to execute post-execution
<% # Databinder. eval (container. dataitem, "integervalue", "{0: c}") %>

. It is generally used to format strings when binding.
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:

. Use this method as few as possible in most cases
<% # (Bool) databinder. eval (container. dataitem, "boolvalue") %>

. Use this method as few as possible in most cases
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.

 

 

Essay-209 articles-0 comments-13

Simple differences: <% # Eval () %>And<% # BIND () %>

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;

The most fundamental difference for programmers is:
1. BindIs updatable, yesBindThe bound column can be editable and can interact with the data source control and directly interact with the database.BindThen, the user-defined operations on the program end will not be available, suchConvert, tostring ()And so on,Or a self-written Function,Not at the program end
2. EvalIs read-only data,EvalIt cannot interact with the data source control, which is one-way, but can be customized.

2. Details

The eval () method uses reflection to perform post-Binding Calculations at runtime. Therefore, compared with the standard ASP. NET data binding method, this causes a significant reduction in performance. 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.

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

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

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" %>

 

Asp.netMediumDatabinder. EvalSummary of Usage

<% # BIND ("subject") %> // bind a field
<% # Container. dataitemindex + 1%> // implement automatic numbering
<% # Databinder. eval (container. dataitem, "[N]") %>
Common method(These three have the best performance.)
<% # Databinder. eval (container. dataitem, "columnname") %>
<% # Databinder. eval (container. dataitem, "columnname", null) %>
<% # Databinder. eval (container, "dataitem. columnname", null) %>
Other usage
<% # (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). Attributes. tostring () %> // If the attribute is of the string type, tostring () is not required.
Databinder. EvalUsage example
<% # Databinder. eval (container. dataitem, "integervalue", "{0: c}") %>
The format string parameter is optional. If the parameter is ignored, databinder. Eval returns the object type value,

// Display two decimal places
<% # Databinder. eval (container. dataitem, "unitprice", "$ {0: F2}") %>

// {0: g} indicates that true or false is displayed.
<Itemtemplate>
<Asp: image width = "12" Height = "12" border = "0" runat = "server"
Alternatetext = '<% # databinder. eval (container. dataitem, "discontinued", "{0: g}") %>'
Imageurl = '<% # databinder. eval (container. dataitem, "discontinued ","~ /Images/{0: g2.16.gif ") %> '/>
</Itemtemplate>

// Conversion Type
(String) databinder. eval (container, "dataitem. p_ship_time_sbm8"). substring (4, 4)
{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format
{0: c} currency Style
<% # Container. dataitem ("price", "{0: ¥ #,## 0.00}") %>
<% # Databinder. eval (container. dataitem, "company_ureg_date", "{0: yyyy-m-d}") %>
Specifier Type format output (passed double 1.42) output (passed int-12400)
C currency {0: c} $1.42-$12,400
D decimal {0: d} system. formatexception-12400
E scientific {0: e} 1.420000e + 000-1.2420.e + 004
F fixed point {0: f} 1.42-12400.00
G General {0: g} 1.42-12400
N number with commas for thousands {0: n} 1.42-12,400
R round trippable {0: R} 1.42 system. formatexception
X hexadecimal {0: X4} system. formatexception cf90

{0: d} date only displays year, month, and day
{0: yyyy-mm-dd} displays year, month, and day by format

The style depends on the settings in Web. config.
{0: c}Or{0:Bytes0,000.00}Currency Style Standard British currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "En-us" uiculture = "En-us"/>
</System. Web>
Displayed as listen 3,000.10

{0: c}OrString. Format ("{0: c}", price );Chinese currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "ZH-CN" uiculture = "ZH-CN"/>
</System. Web>
Shown as ¥3,000.10

{0: c}OrString. Format ("{0: c}", price );American currency Style
<System. Web>
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8"/>
</System. Web>
Shown as $3,000.10

 

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.