Differences between asp.net Eval and Bind

Source: Internet
Author: User
Tags comments eval reflection

Differences between eval and bind
Recently I am working on the asp tutorial. net project. Because of the binding of the database tutorial project, eval and bind are always forgotten. Now baidu has left a mark:

Binding expression

<% # Eval ("field name") %>

<% # Bind ("field name") %>

Major differences:

1. The data obtained by eval is read-only, while the bind is updatable.

2. eval is usually used to format the data into a string, so the performance will decrease compared with the bind method.

3. At runtime, eval 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. For details, refer to the description of the databinder. eval () method in msdn.

Obtain the value of a field through eval. As follows:

Protected void page_load (object sender, eventargs e)
{
List <comment> comments = getcomments ();
This. rptcomments. datasource = comments;
This. rptcomments. databind ();
}

<Asp: repeater runat = "server" id = "rptcomments">
<Itemtemplate>
Title: <% # eval ("title") %>

Conent: <% # eval ("content") %>
</Itemtemplate>
<Separatortemplate>
<Hr/>
</Separatortemplate>
</Asp: repeater>


Here, the eval object obtains the value of the title and content attributes through reflection. As a result, someone often said, "How poor is the performance of reflection? I don't need it !". Here, I still have a reserved attitude towards this kind of practice of pursuing detailed performance. Of course, in the above example, we can do the following:

<Asp: repeater runat = "server" id = "rptcomments">
<Itemtemplate>
Title: <% # (container. dataitem as comment). title %>

Conent: <% # (container. dataitem as comment). content %>
</Itemtemplate>
<Separatortemplate>
<Hr/>
</Separatortemplate>
</Asp: repeater>

We use container. dataitem to obtain the data object in the current traversal process, convert it to comment, and then read its title and content attributes. Although the expression is somewhat long, it seems to be a good solution. Performance ...... It must have been improved.

However, in the actual development process, we may not be able to use a specific type of data as a data source so easily. We often need to combine two types of objects for joint display. For example, when we display the comment list, we usually need to display the personal information of the user. Since Anonymous objects are already supported in c #3.0, we can do this:

Protected void page_load (object sender, eventargs e)
{
List <comment> comments = getcomments ();
List <user> users = getusers ();

This. rptcomments. datasource = from c in comments
From u in users
Where c. userid = u. userid
Order by c. createtime
Select new
                                  {
Title = c. title,
Content = c. content,
Nickname = u. nickname
};
This. rptcomments. databind ();
}

Note: the data item of the current container: container. dataitem

4. Application of the bind () method. Main application scenarios: update operations for editable controls such as gridview. When you click the update button, the attribute values of each control bound with the bind syntax are extracted and passed to the data source control for update.
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 the asp.net tutorial, data binding controls (such as the gridview, detailsview, and formview controls) can be automatically used to update, delete, and insert 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:

This example has a text box used to accept user input, which is a potential security threat. By default, the asp.net web page verifies that user input does not include script or html elements. For more information, see script intrusion overview.

 

<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.

 

Related Article

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.