GridView中的格式化

在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的

<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 >

将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 >

前台代码:

 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 >

附录-常用格式化公式:
{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

转载于:https://www.cnblogs.com/feb9903/archive/2007/04/11/709262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值