Data Binding syntax
The data binding expression is included in the <% # And %> delimiters and uses the eval and bind functions. The eval function is used to define one-way (read-only) binding. The BIND function is used to define bidirectional (updatable) binding. In addition to calling the eval and bind methods in the data binding expression to perform data binding, you can also call any public range within the <% # And %> delimiters.CodeTo execute the code During page processing and return a value.
When the databind method of the control or page class is called, the data binding expression is parsed. Some controls, such as the gridview, detailsview, and formview controls, automatically parse the data binding expression during the prerender event of the control and do not need to explicitly call the databind method.
The following code example demonstrates how to use a data binding expression with the formview control in itemtemplate.
<Asp: formview id = "formview1"
Performanceid = "sqlperformance1"
Datakeynames = "productid"
Runat = "server">
<Itemtemplate>
<Table>
<Tr> <TD align = right> <B> product ID: </B> </TD> <% # eval ("productid ") %> </TD> </tr>
<Tr> <TD align = right> <B> product name: </B> </TD> <% # eval ("productname ") %> </TD> </tr>
<Tr> <TD align = right> <B> category ID: </B> </TD> <% # eval ("categoryid ") %> </TD> </tr>
<Tr> <TD align = right> <B> quantity per unit: </B> </TD> <% # eval ("quantityperunit ") %> </TD> </tr>
<Tr> <TD align = right> <B> unit price: </B> </TD> <% # eval ("unitprice ") %> </TD> </tr>
</Table>
</Itemtemplate>
</ASP: formview>
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"/>
& Nbsp;
<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.
Explicitly call the databind Method
Some controls, such as the gridview, formview, and detailsview controls, are bound to the data source control through the performanceid attribute by implicitly calling the databind method. However, in some cases, you need to explicitly call the databind method to perform binding.
One of the cases is when a control is bound to the data source control using the datasource property instead of the performanceid property. In this case, you need to explicitly call the databind method to execute the data binding and parsing data binding expressions.
another scenario is when you need to manually refresh the data in the data binding control. Suppose there is such a page with two controls, both of which display information from the same database (different views may be used ). In this case, you may need to explicitly rebind the control to the data to keep the data display synchronized. For example, you may have a gridview control that displays the product list and a detailsview control that allows you to edit a single product. Although the data displayed by the gridview and detailsview controls come from the same data source, they are bound to different data source controls because they use different queries to obtain their data. The user may use the detailsview control to update records, causing the associated data source control to execute updates. However, because the gridview control is bound to different data source controls, the control will still display the old record value until the page is refreshed. Therefore, after the detailsview control updates data, you can call the databind method. This causes the gridview control to update its view and re-execute any data binding expression and the Public scope code within the separators <% # And %>. In this way, the gridview control will reflect the updates made by the detailsview control.