Differences between bind and eval in. net

Source: Internet
Author: User

The eval () method uses reflection at runtime to perform post-bound computing. Therefore, compared with the standard Asp.net data binding method bind, this will cause 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.

Bind and eval in Asp.net: 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 and eval in Asp.net: Use the 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

The bind method in the formview control template allows the control to 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> <TD align = right>

<B> First name: </B> </TD> <TD>

<Asp: textbox id = "editfirstnametextbox" runat = "server" text = '<% # BIND ("firstname") %>'/>

</TD> </tr> <TD align = right>

<B> last name: </B> </TD> <TD>

<Asp: textbox id = "editlastnametextbox" runat = "server" text = '<% # BIND ("lastname") %>'/>

</TD> </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> click when the row Update button, the property values of each control bound with the BIND syntax are extracted and passed to the data source control for update.

Bind and eval in Asp.net: 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:

<% # String. format ("{0: c}", (datarowview) container. dataitem) ["integervalue"]) %> compare this syntax with databinder. the eval syntax is compared. The latter has only three parameters: the naming 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 especially useful because both data rows and data fields must be forcibly converted.

Net eval () method Daquan

. NET technology 23:58:35 read 463 comments 0 font size: large and small subscriptions

<% # BIND ("subject") %> // bind a field
<% # Container. dataitemindex + 1%> // implement automatic numbering
<% # Databinder. eval (container. dataitem, "[N]") %>
Common method (the best performance of these three methods)
<% # 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.
Example of databinder. Eval usage
<% # 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: 00000,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} or string. 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} or string. 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.