Data Binding expression syntax (differences between eval and bind)

Source: Internet
Author: User
Data Binding expression syntax

Using the data binding syntax, You can bind the control property value to data and specify a value to search, update, delete, and insert data.

Data Binding syntax

The data binding expression is included in<% #And%>Separator, and useEvalAndBindFunction.EvalA function is used to define one-way (read-only) binding.BindA function is used to define bidirectional (updatable) binding. BesidesEvalAndBindYou can also call<% #And%>Any public range within the delimiterCodeTo execute the code During page processing and return a value.

TheDatabindThe data binding expression is parsed. For some controls, such as the gridview, detailsview, and formview controlsPrerenderAutomatically parses the data binding expression during the event and does not need to be explicitly calledDatabindMethod.

The following code example demonstrates how to associate a data binding expressionFormviewControl.

Copy code in Visual Basic
<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> <TD align = right> <B> product name: </B> </TD> <% # eval ( "Productname" ) %> </TD> </tr> <TD align = right> <B> category ID: </B> </TD> <% # eval ( "Categoryid" ) %> </TD> </tr> <TD align = right> <B> quantity per unit: </B> </TD> <% # eval ( "Quantityperunit" ) %> </TD> </tr> <TD align = right> <B> unit price: </B> </TD> <% # eval ( "Unitprice" ) %> </TD> </tr> </table> </itemtemplate> </ASP: formview>
C # copy code
 
   
    
    
   <% # eval ("productid ") %> 
     <% # eval ("productname ") %> 
     <% # eval ("categoryid ") %> 
     <% # eval ("quantityperunit ") %> 
     <% # eval ("unitprice ") %> 
    
product ID:
product name:
category ID:
quantity per unit:
unit price:
Use the eval Method

EvalMethod to compute data binding controls (suchGridview,DetailsviewAndFormviewControls. At runtime,EvalCall the eval method of the databinder object and reference 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, suchGridviewControl. Therefore, you can only bind data to the template of the control.EvalMethod.

EvalThe 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

BindMethod andEvalMethods have some similarities, but there are also great differences. Although it can be used likeEvalSame MethodBindMethod to retrieve the value of the data binding field, but when the data can be modified, you still need to useBindMethod.

In ASP. NET, data is bound to controls (suchGridview,DetailsviewAndFormviewControls) automatically use the data source controls to update, delete, and insert data. For example, if you have defined SQL select, insert, delete, and update statements for the data source controlGridview,DetailsviewOrFormviewIn the control TemplateBindThe 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 reasonEdititemtemplateOrInsertitemtemplateTo useBindFunction.

BindThe method is usually used with the input control, for exampleGridviewTextbox Control rendered by the row. When the data binding control creates these input controls as a part of its own rendering, this method can extract the input value.

BindThe method uses the name of the data field as a parameter to associate it with the bound property, as shown in the following example:

 
   
   
   
   
   <% # eval ("employeeid ") %> 
    
employee ID:
First name:
Last Name:
& nbsp;

ClickUpdateButton, useBindThe attribute values of each control bound to the syntax are extracted and passed to the data source control for update.

Explicitly call the databind Method

Some controls, suchGridview,FormviewAndDetailsviewControls. When they are bound to the data source control through the performanceid attributeDatabindMethod To perform binding. However, in some cases, explicit calling is required.DatabindMethod To perform binding.

One of the scenarios is to use the datasource property (insteadPerformanceidProperty) When you bind a control to a data source control. In this case, you need to explicitly callDatabindTo bind and parse data.

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,GridviewControls, andDetailsviewControl. AlthoughGridviewAndDetailsviewThe data displayed by the control comes from the same data source, but is bound to different data source controls, because the two controls use different queries to obtain their data. Users may useDetailsviewControl update record, causing the associated data source control to perform updates. HoweverGridviewThe control is bound to different data source controls. Therefore, the control displays the old record value until the page is refreshed. ThereforeDetailsviewAfter the control updates data, you can call the databind method. This causesGridviewControl to update its view, and re-execute any data binding expression and<% #And%>The code within the delimiter. In this way,GridviewControl will reflectDetailsviewControls.

Http://msdn2.microsoft.com/zh-cn/library/ms178366 (vs.80). aspx

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.