| 发表于:2008-02-27 16:03:294楼 得分:0 |
10.gridview实现自定义时间货币等字符串格式: 效果图: 图1-未格式化前 图2-格式化后 解决方法: 在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的 - html code
<asp :boundfield datafield="creationdate"
dataformatstring="{0:m-dd-yyyy}"
headertext="creationdate" />
主要是由于htmlencode属性默认设置为true,已防止xss攻击,安全起见而用的,所以,可以有以下两种方法解决 1、 - html code
<asp :gridview id="gridview1" runat="server">
<columns>
<asp :boundfield datafield="creationdate"
dataformatstring="{0:m-dd-yyyy}"
htmlencode="false"
headertext="creationdate" />
</columns>
</asp>
将htmlencode设置为false即可 另外的解决方法为,使用模版列 - html code
<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>
前台代码: <- html code
asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datakeynames="身份证号码"
datasourceid="sqldatasource1" allowsorting="true" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px" cellpadding="3" font-size="12px" onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="身份证号码" headertext="身份证号码" readonly="true" sortexpression="身份证号码" />
<asp:boundfield datafield="姓名" headertext="姓名" sortexpression="姓名" />
<asp:boundfield datafield="邮政编码" headertext="邮政编码" sortexpression="邮政编码" />
<asp:boundfield datafield="出生日期" headertext="出生日期" sortexpression="出生日期" />
<asp:boundfield datafield="起薪" headertext="起薪" sortexpression="起薪" />
</columns>
<footerstyle backcolor="white" forecolor="#000066" />
<rowstyle forecolor="#000066" />
<selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" />
<pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" />
<headerstyle backcolor="#006699" font-bold="true" forecolor="white" />
</asp:gridview>
<asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:北风贸易connectionstring1 %>"
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 | | |
|