<asp :BoundField DataField="CreationDate"
DataFormatString="{0:M-dd-yyyy}"
HeaderText="CreationDate" />
主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决
1、
1
<
asp
:GridView ID
="GridView1"
runat
="server"
>
2 < columns >
3 < asp :BoundField DataField ="CreationDate"
4 DataFormatString ="{0:M-dd-yyyy}"
5 HtmlEncode ="false"
6 HeaderText ="CreationDate" />
7 </ columns >
8 </ asp >
2 < columns >
3 < asp :BoundField DataField ="CreationDate"
4 DataFormatString ="{0:M-dd-yyyy}"
5 HtmlEncode ="false"
6 HeaderText ="CreationDate" />
7 </ columns >
8 </ asp >
将htmlencode设置为false即可
另外的解决方法为,使用模版列
1
<
asp
:GridView ID
="GridView3"
runat
="server"
>
2 < columns >
3 < asp :TemplateField HeaderText ="CreationDate" >
4 < edititemtemplate >
5 < asp :Label ID ="Label1" runat ="server"
6 Text ='<%# Eval("CreationDate", "{0:M-dd-yyyy}") % > '>
7 </ asp >
8 </ edititemtemplate >
9 < itemtemplate >
10 < asp :Label ID ="Label1" runat ="server"
11 Text =’<%# Bind("CreationDate", "{0:M-dd-yyyy}") % > '>
12 </ asp >
13 </ itemtemplate >
14 </ asp >
15 </ columns >
16 </ asp >
2 < columns >
3 < asp :TemplateField HeaderText ="CreationDate" >
4 < edititemtemplate >
5 < asp :Label ID ="Label1" runat ="server"
6 Text ='<%# Eval("CreationDate", "{0:M-dd-yyyy}") % > '>
7 </ asp >
8 </ edititemtemplate >
9 < itemtemplate >
10 < asp :Label ID ="Label1" runat ="server"
11 Text =’<%# Bind("CreationDate", "{0:M-dd-yyyy}") % > '>
12 </ asp >
13 </ itemtemplate >
14 </ asp >
15 </ columns >
16 </ asp >
前台代码:
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
DataKeyNames
="身份证号码"
2 DataSourceID ="SqlDataSource1" AllowSorting ="True" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" CellPadding ="3" Font-Size ="12px" OnRowDataBound ="GridView1_RowDataBound" >
3 < Columns >
4 < asp:BoundField DataField ="身份证号码" HeaderText ="身份证号码" ReadOnly ="True" SortExpression ="身份证号码" />
5 < asp:BoundField DataField ="姓名" HeaderText ="姓名" SortExpression ="姓名" />
6 < asp:BoundField DataField ="邮政编码" HeaderText ="邮政编码" SortExpression ="邮政编码" />
7 < asp:BoundField DataField ="出生日期" HeaderText ="出生日期" SortExpression ="出生日期" />
8 < asp:BoundField DataField ="起薪" HeaderText ="起薪" SortExpression ="起薪" />
9 </ Columns >
10 < FooterStyle BackColor ="White" ForeColor ="#000066" />
11 < RowStyle ForeColor ="#000066" />
12 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
13 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
14 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
15 </ asp:GridView >
16 < asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
17 SelectCommand ="SELECT top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]" DataSourceMode ="DataReader" ></ asp:SqlDataSource >
2 DataSourceID ="SqlDataSource1" AllowSorting ="True" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" CellPadding ="3" Font-Size ="12px" OnRowDataBound ="GridView1_RowDataBound" >
3 < Columns >
4 < asp:BoundField DataField ="身份证号码" HeaderText ="身份证号码" ReadOnly ="True" SortExpression ="身份证号码" />
5 < asp:BoundField DataField ="姓名" HeaderText ="姓名" SortExpression ="姓名" />
6 < asp:BoundField DataField ="邮政编码" HeaderText ="邮政编码" SortExpression ="邮政编码" />
7 < asp:BoundField DataField ="出生日期" HeaderText ="出生日期" SortExpression ="出生日期" />
8 < asp:BoundField DataField ="起薪" HeaderText ="起薪" SortExpression ="起薪" />
9 </ Columns >
10 < FooterStyle BackColor ="White" ForeColor ="#000066" />
11 < RowStyle ForeColor ="#000066" />
12 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
13 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
14 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
15 </ asp:GridView >
16 < asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
17 SelectCommand ="SELECT top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]" DataSourceMode ="DataReader" ></ asp:SqlDataSource >
附录-常用格式化公式:
{0:C} 货币;
{0:D4}由0填充的4个字符宽的字段中显示整数;
{0:000.0}四舍五入小数点保留第几位有效数字;
{0:N2}小数点保留2位有效数字;{0:N2}% 小数点保留2位有效数字加百分号;
{0:D}长日期;{0:d}短日期;{0:yy-MM-dd} 例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25