GridView實現自定義時間貨幣等字符串格式
10.GridView實現自定義時間貨幣等字符串格式:
效果圖:圖1-未格式化前
效果圖:圖2-格式化後
解決方法:
在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:GridView>
將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:Label>
</edititemtemplate>
<itemtemplate>
<asp :Label ID="Label1" runat="server" Text=’<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp:Label>
</itemtemplate>
</asp:TemplateField>
</columns>
</asp:GridView> 前臺代碼: <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
</asp:GridView> 前臺代碼: <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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.