जगदीश खोलिया: Using the Eval and Bind Method

Thursday, June 16, 2011

Using the Eval and Bind Method

Using the Eval Method



The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval method of the DataBinder
object, referencing the current data item of the naming container. The
naming container is generally the smallest part of the data-bound
control that contains a whole record, such as a row in a GridView control. You can therefore use the Eval method only for binding inside templates of a data-bound control.


The Eval method takes the
name of a data field and returns a string containing the value of that
field from the current record in the data source. You can supply an
optional second parameter to specify a format for the returned string.
The string format parameter uses the syntax defined for the Format method of the String class.



Using the Bind Method



The Bind method has some similarities to the Eval method, but there are significant differences. Although you can retrieve the values of data-bound fields with the Bind method, as you can with the Eval method, the Bind method is also used when data can be modified.


In ASP.NET, data-bound controls such as the gridview, detailsview, and formview
controls can automatically use the update, delete, and insert
operations of a data source control. For example, if you have defined
SQL Select, Insert, Delete, and Update statements for your data source
control, using Bind in a gridview, detailsview, or formview

control template enables the control to extract values from child
controls in the template and pass them to the data source control. The
data source control in turn performs the appropriate command for the
database. For this reason, the Bind function is used inside the EditItemTemplate or InsertItemTemplate of a data-bound control.


The Bind method is typically used with input controls such as the textbox control rendered by a gridview
row in edit mode. When the data-bound control creates these input
controls as part of its own rendering, it can extract the input values.

No comments: