对模版列的格式化输出

最近我在研究ASP.NET中,发现在对模版列进行输出时,可以这样做。有的时候要对模版列的某些输出进行格式化输出,比如

QuarterP & L Statement
Q1 2001
------
Profit:102200111
Q2 2001
------
Profit:-154950
Q3 2001
------
Profit:-902200

这个表里,如果要对Revenue,profit为负数时,予以红色显示,可以这样做:

1<asp:datagrid autogeneratecolumns="False" runat="server"> <columns> <asp:boundcolumn datafield="Quarter" headertext="Quarter"></asp:boundcolumn> <asp:templatecolumn headertext="P &amp; L Statement"> <itemtemplate> <table border="0"> <tr> <td align="right"><b>Revenue:</b></td> <td>```
2# **_MakeNegRed_ (DataBinder.Eval(Container.DataItem, "Revenue"))** 
3```</td> </tr> <tr> <td align="right"><b>Profit:</b></td> <td>```
4# **_MakeNegRed_ (DataBinder.Eval(Container.DataItem, "Profit"))** 
5```</td> </tr> </table> </itemtemplate> </asp:templatecolumn> </columns> </asp:datagrid>

其中,MakeNegRed是个函数,可以这样写,返回的是HTML。

Function MakeNegRed(input as String) as String 'See if the number is less than 0 If Int32.Parse(input) &lt; 0 then Return "
1<font color="" red""="">" &amp; input &amp; "</font>

" Else Return input End If End Function

哈哈,这样就达到效果拉。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus