小tips:asp.net2.0中在gridview中使用DataFromatString

来源:互联网 发布:cs架构b2b源码 编辑:程序博客网 时间:2024/06/06 03:17
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
可能之前不少朋友也已经试过,但我是今天才遇到这个问题,翻查资料后才解决。主要是
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

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击