Today, when I helped a customer deploy a website, I found that there were several differences in the website that was previously well tuned locally, because local development was in the vs 2010 environment, the configuration of the customer space and the domain name is different from that of the local environment;
One problem is that dataformatstring in the gridview is invalid;
Solution:
When we obtain data entities from the business logic layer, the next thing is to bind them to the control. Some fields in the data object can be directly bound to the interface, but some fields need to be reformatted. For example, for the currency unit field, you need to display the currency symbol and the display separator at every three places; for example, for the date field, the database stores the date and time, however, the page is displayed in the format of xxxx, XX, and XX. At this time, we used Dataformatstring Attribute.
<ASP:Gridview ID= "Grvresult" Runat= "Server" Autogeneratecolumns= "False" Width= "100%">
<Columns>
<ASP:Boundfield Headertext="Reservation date" Datafield= "Operationdate" Dataformatstring= "{0: yyyy-mm-dd }" Htmlencode= "False">
</ASP:Boundfield>
<ASP:Boundfield Headertext="Total orders" Datafield= "Totalrate" Dataformatstring= "{0: c }" Htmlencode= "False">
</ASP:Boundfield>
</Columns>
</ASP:Gridview>
For exampleCodeThe date and currency binding modes are displayed.DataformatstringIn{0}It is a fixed format, which is used with {0} In string. fromat ("{0}", somestring) to indicate the parameter index number of the bound context. Then, add a formatted string to the end. For detailed usage instructions, refer to msdn.
Note the following points:
1. In the gridviewASP:BoundfieldUseDataformatstringAttribute must be setHtmlencode= "False"Otherwise, it does not work.
2. If you want to use a formatted string of the date type, the corresponding fields in the data object must also be of the date type.
3. Format String C to indicate the currency unit. The data type to bind should be numeric. If the string type does not work, you need to manually add the format stringDataformatstring="¥{0: c }".
Summary:
Using datafromatstring in the gridview is somewhat different from using it in the DataGrid! The htmlencode attribute is added to boundfield in the gridview and the default value is true, which invalidates datafromatstring!