asp.net data binding common code

Source: Internet
Author: User
asp.net| data

Binding with DataBinder.Eval does not necessarily concern the data source (Dataread or DataSet). You don't have to care about the type of data. Eval converts this data object to a string. A lot of work was done on the bottom bound, using reflective performance. It is easy to use, but it affects the performance of the data. When the dataset is bound, the DataItem is actually a datarowview (if the binding is a data reader (Dataread) it is a idatarecord. Therefore, the direct conversion to DataRowView will give a great boost to performance.
Note When using: 1. Note The case of the field name (pay special attention to). If inconsistent with the query, in some cases it can lead to slower than <%# DataBinder.Eval (Container.DataItem, field name)%>. 2. If the speed is to be further improved, the method of <%# ((System.Data.DataRowView) Container.DataItem) [0]%> can be used. But its readability is not high.

When you make a dataset as a data source

Normal (C #)

<%# ((System.Data.DataRowView) container.dataitem) ["Title"]%>


Bound Date field format string

<%# Convert.todatetime ((System.Data.DataRowView) Container.DataItem)

["Fbtime"]). ToString ("YYYY year mm month DD Day")%>

<%# DataBinder.Eval (Container, "Dataitem.fbtime", "{0:yyyy year mm month DD day}")%>


String binding exceeds specified length truncation

<%# DataBinder.Eval (Container.DataItem, "Text"). ToString (). Trim (). Length>7?

DataBinder.Eval (Container.DataItem, "Text"). ToString (). Trim (). Substring

(0,7):D atabinder.eval (Container.DataItem, "Text"). ToString (). Trim ()%>


Price grid try to 2 decimal places

<%# Convert.todecimal ((System.Data.DataRowView) container.dataitem) ["Price"]). ToString ("F2")%>

<%# DataBinder.Eval (Container, "Dataitem.price", "{0:F2}")%>
' VB.net the price after the format of the decision
<%# IIf (DataBinder.Eval (Container.DataItem, "price", "{0:f2}") = "0.00", "Negotiable", DataBinder.Eval (Container.DataItem, "Price", "{0:F2}") + "Million")%>

' VB.net routine
<%# Ctype (Container.dataitem,datarowview). Row ("Title")%>



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.