设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

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

作者:佚名 出处:网络转载 时间:08-29 点击:

内容载入中...
华腾联合陪你一路学习 吴龙杰

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

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息