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 the server is returned. The server can process the changed data, such as saving the data to the database.
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 difference between the Bind method and the Eval method is that the Bind method can read and update data, but the Eval method can only read the displayed 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.
In the update operation, we can operate in the gridviewdomainrowupdating event, for example:
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
// Update the row GridViewRow
GridViewRow row = this. GridView1.Rows [e. RowIndex];
// Search for updated controls
DropDownList present = (DropDownList) row. FindControl ("ddlPresent ");
TextBox price = (TextBox) row. FindControl ("txtPrice ");
TextBox updated = (TextBox) row. FindControl ("txtUpdated ");
// Update
E. NewValues ["present"] = present. SelectedValue;
E. NewValues ["price"] = price. Text;
E. NewValues ["updated"] = updated. Text;
}
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
// Update the row GridViewRow
GridViewRow row = this. GridView1.Rows [e. RowIndex];
// Search for updated controls
DropDownList present = (DropDownList) row. FindControl ("ddlPresent ");
TextBox price = (TextBox) row. FindControl ("txtPrice ");
TextBox updated = (TextBox) row. FindControl ("txtUpdated ");
// Update
E. NewValues ["present"] = present. SelectedValue;
E. NewValues ["price"] = price. Text;
E. NewValues ["updated"] = updated. Text;
}
If we can fully understand the Bind method and Eval method, we can do it automatically without having to write it as above. In addition to complicated operations, the above method is also a usage skill.
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 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" %>
Eval details
<% # 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 Tutorial: 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