ASP four ways to bind

Source: Internet
Author: User

1. Data binding syntax in 1.x

<asp:literal id= "LitEval2" runat= "server" text= ' <% #DataBinder. Eval (Container.DataItem, "UserName")%> '/ >

2.2.x simplified eval data binding syntax

<asp:literal id= "LitEval1" runat= "server" text= ' <%eval ("UserName")%> '/>

3. Method overloading of the second method

<a href= ' <%# eval ("UserId", "default.aspx?id={0}")%> ' ><%# eval ("UserName")%></a>

4. Eval binds two values at a time

<a href= ' <%# string. Format ("Default.aspx?id={0}&role={1}", eval ("UserId"), eval ("Userrole"))%> ' ><%# eval ("UserName")% ></a>

The Eval_r () method uses reflection to perform late-bound computations at run time, which results in a noticeable decrease in performance compared to the standard ASP. NET data binding method. It is typically used when binding requires formatting a string. Use this method sparingly in most cases

The Eval method is a static (read-only) method that takes the value of a data field as a parameter and returns it as a string. The Bind method supports read/write functionality that retrieves the value of a data-bound control and submits any changes back to the database.

Using the Eval method
The Eval method calculates a late-bound data expression in a template for data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method invokes the Eval method of the DataBinder object, referencing the current data item of the naming container. A naming container is typically the smallest part of a data-bound control that contains a complete record, such as a row in a GridView control. Therefore, you can only use the Eval method on bindings within the template of a data-bound control.

The Eval method returns a string containing the value of the field from the current record of the data source with the name of the data field as an argument. You can provide a second argument that specifies the format of the returned string, which is an optional parameter. The string format parameter uses the syntax defined for the format method of the string class.

Using the Bind method
There are some similarities between the Bind method and the Eval method, but there are also significant differences. Although you can use the Bind method to retrieve the value of a data-bound field as you would with the Eval method, use the Bind method when the data can be modified.

Data-bound controls, such as the GridView, DetailsView, and FormView controls, automatically use the update, delete, and insert operations of the data source control in ASP. For example, if you have defined SQL Select, Insert, Delete, and Update statements for a data source control, you can make the control from a child control in the template by using the Bind method in the GridView, DetailsView, or FormView control template Extracts the values and passes them to the data source control. The data source control then executes the appropriate database commands. For this reason, the bind function is used in the EditItemTemplate or insertitemtemplate of a data-bound control.

The Bind method is typically used with an input control, such as a TextBox control rendered by a GridView row in edit mode. When data-bound controls create these input controls as part of their own rendering, the method can extract the input values.

The Bind method takes the name of the data field as an argument and is associated with the binding property, as shown in the following example:

<EditItemTemplate>
<table>
<tr>
&LT;TD align=right>
<b>employee id:</b>
</td>
<td>
<%# eval_r ("EmployeeID")%>
</td>
</tr>
<tr>
&LT;TD align=right>
<b>first name:</b>
</td>
<td>
<asp:textbox id= "Editfirstnametextbox" runat= "Server"
Text= ' <%# Bind ("FirstName")%> '/>
</td>
</tr>
<tr>
&LT;TD align=right>
<b>last name:</b>
</td>
<td>
<asp:textbox id= "Editlastnametextbox" runat= "Server"
Text= ' <%# Bind ("LastName")%> '/>
</td>
</tr>
<tr>
&LT;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 for a row, each control property value that is bound with the bind syntax is extracted and passed to the data source control to perform the update operation.

Using DataBinder.Eval
ASP. NET provides a static method named DataBinder.Eval that computes a late-bound data-binding expression and formats the result as a string (optional). With this approach, you can avoid many explicit casts that you must perform when you force a value to the desired data type.

For example, in the following code fragment, an integer is displayed as a currency string. Using the standard ASP. NET data binding syntax, you must first cast the data row type to retrieve the data field IntegerValue. This is then passed as a parameter to the String.Format method:

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

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

<%# 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:
<%# (BOOL) databinder.eval_r (Container.DataItem, "Boolvalue")%>
DataBinder.Eval is particularly useful when data binding is performed on controls in a templated list, because data rows and data fields are usually forced to be cast.

The bind and Eval methods can be used in the TemplateField template in order to restrict or remove values from a column in the database. The following is the format of the Bind method, and the format of Eval is the same as bind. Bind ("Name of column", "Formatted text displayed")

For example, we want to take a date type of data, in the database column name is updated, the value is 2008/06/01. But like June 01, 2008, we can write bind ("updated", "{0:yyyy mm month DD Day}"), and Eval is the same.

Both can read the values in the data and display them. When we use the edit update operation, bind can automatically update the modified value to the database and display the modified value. But with Eval you can only get the wrong picture, and 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 read and update data in 2 ways, but the Eval method can only read the displayed data. Therefore, when we choose the Bind method and the Eval method, we must have the contention, when the data must need to update the operation when we should use BIND, just display the data, there is no action can use the Eval method.

In the update operation we can operate in the Gridview1_rowupdating event, as in the following example:

If we can fully understand the bind method and the Eval method, in fact there is no need to write to the above, can be done automatically. The above method will be used in addition to more complex operations, which is also a skill to use.

ASP four ways to bind

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.