小tips:asp.net 2.0中在gridview中使用DataFromatString

王朝asp·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

可能之前不少朋友也已经试过,但我是今天才遇到这个问题,翻查资料后才解决。主要是

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

<asp :BoundField DataField=“CreationDate”

DataFormatString=“{0:M-dd-yyyy}”

HeaderText=“CreationDate” />

主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决

1、

<asp :GridView ID=“GridView1″ runat=“server”>

<columns>

<asp :BoundField DataField=“CreationDate”

DataFormatString=“{0:M-dd-yyyy}”

HtmlEncode=“false”

HeaderText=“CreationDate” />

</columns>

</asp>

将htmlencode设置为false即可

另外的解决方法为,使用模版列

<asp :GridView ID=“GridView3″ runat=“server” >

<columns>

<asp :TemplateField HeaderText=“CreationDate” >

<edititemtemplate>

<asp :Label ID=“Label1″ runat=“server”

Text=‘<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>‘>

</asp>

</edititemtemplate>

<itemtemplate>

<asp :Label ID="Label1" runat="server"

Text=’<%# Bind(“CreationDate”, “{0:M-dd-yyyy}”) %>‘>

</asp>

</itemtemplate>

</asp>

</columns>

</asp>

http://www.cnblogs.com/jackyrong/archive/2006/08/28/488282.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航