Format or format data in Eval ()

Source: Internet
Author: User
Tags eval string format

<%# Eval ("schoolend") = = DBNull.Value? "": Convert.todatetime (Eval ("Schoolend")). ToString ("Yyyy-mm-dd")%>

With no null
<%# (DateTime) Eval ("Se_start_date"). ToString ("Yyyy-mm-dd")%>

<% #Eval ("Begindate", "{0:YYYY-MM-DD}")%>

, why set the dataformatstring has no effect.

Do not ignore the two important attributes of BoundField HTMLEncode and Applyformatineditmode.


HtmlEncode


The BoundField used by the GridView differs from the DataGrid using BoundColumn, Bounfield provides a HtmlEncode attribute that provides HTML encoding of the data and reduces the BoundColumn potential HTML &script embedded attack, the default value is enabled. If you want to use formatted output, you should turn off this property.


<asp:boundfield datafield= "HireDate" dataformatstring= "{0:yyyy year M month D-day}" Htmlencode= "false" headertext= "HireDate"/ >
Applyformatineditmode


By default, formatting strings can be applied to field values only if the data-bound control is in read-only mode. To apply a formatted string to the displayed value when the data-bound control is in edit mode, you should also set the Applyformatineditmode property to True.


<asp:boundfield datafield= "HireDate" dataformatstring= "{0:yyyy year M month D Day}" Htmlencode= "false" headertext= "HireDate" Applyformatineditmode= "true"/>


2, the dataformatstring format


The formatted string can be any string, and typically contains placeholders for field values.


For example: dataformatstring= "AAA{N:BBB}CCC", where AAA and CCC represent arbitrary strings; n is the index of the field value in the zero-based argument list because there is only one field value in each cell, so n is usually 0 BBB for the format string represents the format that you want the data to display.


3. Common format type of GridView data


Number {0:N2} 12.36


Number {0:n0} 13


Number {0:d} 12345 12345


Number {0:d8} 12345 00012345


Number {0:f} 12345.6789 12345.68


Number {0:f0} 12345.6789 12346


Number {0:g} 12345.6789 12345.6789


Number {0:g7} 123456789 1.234568E8


Currency {0:C2} $12.36


Currency {0:C4} $12.3656


Currency "¥{0:n2}" ¥12.36


Scientific counting method {0:e3} 1.23E+001


Percent {0:P} 12.25%


Date {0:d} November 25, 2006


Date {0:d} 2006-11-25


Date {0:f} November 25, 2006 10:30


Date {0:f} November 25, 2006 10:30:00


Date {0:s} 2006-11-26 10:30:00


Time {0:t} 10:30:00


Time {0:t} 10:30


HyperLinkField


Specifically, HyperLinkField is the result of implementing a multiple-parameter formatted link that is not supported by the hyperlinkcolumnd of the DataGrid. Usually we are attached to the URL behind the querystring will not have only one, asp.net 1. Only bound columns are used in X, and code is written manually:


<asp:datagrid id= "DATAGRID1" runat= "Server" datasourceid= "SqlDataSource1" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:hyperlink runat= "Server" text= "View Photo" Navigateurl= ' <%# String.Format ("Photo.aspx?empid={0}&path ={1} ", eval (" EmployeeID "), eval (" Photopath "))%> ' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Now use HyperLinkField, look down, save a lot of Coolie live:)


Copy Save


<asp:gridview id= "GridView1" runat= "Server" allowpaging= "true" allowsorting= "true"
autogeneratecolumns= "False" datakeynames= "EmployeeID" datasourceid= "SqlDataSource1" >
<Columns>
<asp:hyperlinkfield datanavigateurlfields= "employeeid,city" datanavigateurlformatstring= "photo.aspx?empid={0}" &path={1} "
headertext= "Photopath" text= "View Photo"/>
</Columns>
</asp:GridView>
Attention:


1. Multiple Field use in DataNavigateUrlFields in. aspx, separated by (comma)


2, Yu-bound URL actual value, can not be a complete absolute path (such as: http://www.cnblogs.com/Jinglecat/archive/2007/05/20/753284.html), Instead, you should provide a relative path (such as: jinglecat/archive/2007/05/20/753284.html), otherwise the URL will not be output in the whole, should be HyperLinkField internal HTML monitoring, But it does not provide switch properties such as BoundField HTMLEncode to the developer, should be a bug bar.


Null value (NULL) processing


If the value of the field is blank, you can display the custom caption by setting the NullDisplayText property.


You can also automatically convert an empty string ("") field value to a null value by setting the ConvertEmptyStringToNull property to the True,boundfield object.




1 Preface


If you are familiar with the WTL or CString standard of the Cstring,windows Template Library (Template) of Microsoft Foundation Classes (MFC) Library (STL) String class, then you must be familiar with the String.Format method. This method is often used in C # to format strings, such as the following:


int x = 16;
Decimal y = 3.57m;
String h = String.Format ("Item {0} sells at {1:c}", X, y);
Console.WriteLine (h);
On my machine, I can get the following output:


Item sells at¥3.57
Maybe the output on your machine is not the same as this one. This is normal, and this article will explain the problem later.


In our daily use, more is the use of the Console.WriteLine method to output a string. In fact, String.Format and Console.WriteLine have a lot in common. Two methods have many overloaded formats and take an array of objects with no fixed arguments as the last argument. The following two statements produce the same output.


Console.WriteLine ("Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, True, ' Q ', 4, 5, 6, 7, ' 8 ');
string u = String.Format ("Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}"), 123, 45.67, True, ' Q ', 4, 5, 6, 7, ' 8 ');
Console.WriteLine (u);
The output is as follows:


Hello 123 45.67 True Q 4 5 6 7 8
Hello 123 45.67 True Q 4 5 6 7 8
2 string format


String.Format and WriteLine All follow the same formatting rules. The formatted format is as follows: "{N [, M] [: formatstring]}", Arg1, ... argn, in this format:


1) n is an integer starting at 0, representing the number of parameters to format


2 m is an optional integer that represents the width of the formatted parameter, if M is a negative number, the formatted value is left-aligned, and if M is a positive number, the formatted value is right-aligned


3) formatstring is another optional parameter that represents the format code


Argn represents the expression to be formatted, and n is the corresponding.


If the argn is a null value, replace it with an empty string. If there is no formatstring, then the parameter n corresponds to the ToString method to format. The following statement produces the same output:


public class Testconsoleapp
{
public static void Main (string[] args)
{
Console.WriteLine (123);
Console.WriteLine ("{0}", 123);
Console.WriteLine ("{0:d3}", 123);
}
}
The output is:


123
123
123
You can also get the same output through String.Format.


string s = String. Format ("123");
String t = String. Format ("{0}", 123);
string u = String. Format ("{0:d3}", 123);
Console.WriteLine (s);
Console.WriteLine (t);
Console.WriteLine (u);
Therefore, the following conclusions are drawn:


(, M) determines the width and alignment of the formatted string


(: formatstring) determines how data is formatted, such as a currency symbol, a scientific notation, or a 16-way system. Just like the following:


Console.WriteLine ("{0,5} {1,5}", 123, 456); Align Right
Console.WriteLine ("{0,-5} {1,-5}", 123, 456); Left-aligned
Output is


123 456
123 456
You can also merge these expressions, put a comma first, and then place a colon. Just like this:


Console.WriteLine ("{0,-10:d6} {1,-10:d6}", 123, 456);
The output is:

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.