ASP. NET DropDownList1 data binding efficiency

Source: Internet
Author: User

The Helper House (www.bkjia.com) Tutorial saw a test comment on data binding in the CodeProject. This comment has been recognized by the MVP and I am very surprised by this result. It seems that, the habit of writing code will be slightly changed in the future.

First, the test code is provided:

Reference content is as follows:
Public void initList (DataTable dt)
{
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "empName ";
DropDownList1.DataValueField = "empNumber ";
DropDownList1.DataBind ();
}

Public void initList2 (DataTable dt)
{
Foreach (DataRow r in dt. Rows)
{
DropDownList2.Items. Add (new ListItem (r ["empName"]. ToString (), r ["empNumber"]. ToString ()));
}
}

Public void initList3 (DataTable dt)
{
Foreach (DataRow r in dt. Rows)
{
DropDownList3.Items. Add (new ListItem (r [0]. ToString (), r [1]. ToString ()));
}
}

The efficiency of initList is more than 10 times lower than that of initList2 and initList3 (It's terrible. I haven't tested it carefully, but it's probably a bit exaggerated). initList2 and initList3 are almost the same in efficiency, however, the efficiency of initList3 is higher than that of initList2, because the code using the column number does not need to be converted to the corresponding subscript when bound, and the data in the associated data source does not need to be converted to the corresponding subscript. also, Cache is used when column numbers are used, but not every time. therefore, data binding using initList3 is the most efficient.

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.