Container. DataItem is faster than Eval in ASP. NET

Source: Internet
Author: User

In ASP. NET, the databinding expression <% # some expression %> is evaludated during the data binding phase.
You can use two ways to write the evaluation expressions. However, it's recommended that we use "Container. DataItem" for performance reason.
For example, the data source is a collection of stronugly-typed objects, "Employee" objects.

public class Employee{ private string _name;  public string Name { get{ return _name; } set{ _name = value; } }}

Eval:

<asp:Repeater runat="server" id="MyRepeater1"> <ItemTemplate> <!-- Using Eval --> <%# Eval("Name") %> </ItemTemplate></asp:Repeater>

OrContainer. DataItem:

<asp:Repeater runat="server" id="MyRepeater2"> <ItemTemplate> <!-- Using Container.DataItem --> <%# ((Employee)Container.DataItem).Name %> </ItemTemplate></asp:Repeater>

As we known, the code above will be finally compiled into an assembly. the assembly will be stored a directory under "Temporary ASP. NET Files ". its name and directory name are both randomly generated by ASP. NET system. for example: C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ Temporary ASP. NET Files \Webapp name\ 050f3065 \ 32a6c804 \ App_Web_xcctyfc6.dll. The full path can be determined by the C # statement:

System.Web.Compilation.BuildManager.GetCompiledAssembly( Request.FilePath ).Location

Let's take a look at the code with the help of a free tool calledReflector. Then we'll know how the piece of code will run on web servers.

Eval:

DataBoundLiteralControl control = (DataBoundLiteralControl) sender;RepeaterItem bindingContainer = (RepeaterItem) control.BindingContainer;control.SetDataBoundString(0, Convert.ToString(base.Eval("Value"), CultureInfo.CurrentCulture));

Eval was defined in System. Web. UI. TemplateControl, and it will finally call System. Web. UI. DataBinder. Eval method.

Container. DataItem:

DataBoundLiteralControl control = (DataBoundLiteralControl) sender;RepeaterItem bindingContainer = (RepeaterItem) control.BindingContainer;control.SetDataBoundString(0, Convert.ToString(((Employee) bindingContainer.DataItem).Value, CultureInfo.CurrentCulture));

"Eval" evaludates the specified expression using reflection technology. "Container. dataItem "just need a simple casting operation. the latter one will obviusly cost less resouces, and as a result have a better performance. this is also proven by my own tests. I found that "Container. dataItem "is always 7 to 10 times faster than" Eval ".

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.