一些数据格式化-Eval( )和DataBinder.Eval(Container.DataItem, )的区别及用法
ASP.NET 2.0改善了模板中的數據綁定操作,把v1.x中的數據綁定語法DataBinder.Eval(Container.DataItem, fieldname)簡化為Eval(fieldname)。Eval方法與DataBinder.Eval一樣可以接受一個可選的格式化字符串參數??s短的Eval語法與DataBinder.Eval的不同點在于,Eval會根據最近的容器對象(例如DataListItem)的DataItem屬性來自動地解析字段,而DataBinder.Eval需要使用參數來指定容器。由于這個原因,Eval只能在數據綁定控件的模板中使用,而不能用于 Page(頁面)層。當然,ASP.NET 2.0頁面中仍然支持DataBinder.Eval,你可以在不支持簡化的Eval語法的環境中使用它。
Asp.net中DataBinder.Eval用法的總結 <%# Bind("Subject") %> //綁定字段 <%# Container.DataItemIndex + 1%> //實現自動編號 <%# DataBinder.Eval(Container.DataItem, "[n]") %> 通常使用的方法(這三個性能最好) <%# DataBinder.Eval(Container.DataItem, "ColumnName") %> <%# DataBinder.Eval(Container.DataItem, "ColumnName", null) %> <%# DataBinder.Eval(Container, "DataItem.ColumnName", null) %> 其他用法 <%# ((DataRowView)Container.DataItem)["ColumnName"] %> <%# ((DataRowView)Container.DataItem).Row["ColumnName"] %> <%# ((DataRowView)Container.DataItem)["adtitle"] %> <%# ((DataRowView)Container.DataItem)[n] %> <%# ((DbDataRecord)Container.DataItem)[0] %> <%# (((自定義類型)Container.DataItem)).屬性.ToString() %>//如果屬性為字符串類型就不用ToString()了 DataBinder.Eval用法范例 <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %> 格式化字符串參數是可選的。如果忽略參數,DataBinder.Eval 返回對象類型的值,
//顯示二位小數 <%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>
//{0:G}代表顯示True或False <ItemTemplate> <asp:Image Width="12" Height="12" Border="0" runat="server" AlternateText='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %>' ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "~/images/{0:G}.gif") %>' /> </ItemTemplate>
//轉換類型 ((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4) {0:d} 日期只顯示年月日 {0:yyyy-mm-dd} 按格式顯示年月日 {0:c} 貨幣樣式 <%#Container.DataItem("price","{0:¥#,##0.00}")%> <%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%> Specifier Type?????? Format???? Output (Passed Double 1.42)??? Output (Passed Int -12400)
c??? Currency????????? {0:c}?????? $1.42?????? -$12,400 d??? Decimal?????????? {0:d}????? System.FormatException??? -12400 e??? Scientific??????? {0:e}????? 1.420000e+000????? -1.240000e+004 f??? Fixed point?????? {0:f}??? 1.42????? -12400.00 g??? General?????????? {0:g}??? 1.42?????? -12400 n??? Number with commas for thousands??? {0:n}??? 1.42?????? -12,400 r??? Round trippable????? {0:r}??? 1.42?????? System.FormatException x??? Hexadecimal????? {0:x4}??? System.FormatException???? cf90
{0:d} 日期只顯示年月日 {0:yyyy-mm-dd} 按格式顯示年月日
樣式取決于 Web.config 中的設置 {0:c}??? 或 {0:£0,000.00} 貨幣樣式??? 標準英國貨幣樣式 <system.web> <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" uiCulture="en-US" /> </system.web> 顯示為 £3,000.10
{0:c}??? 或 string.Format("{0:C}", price); 中國貨幣樣式 <system.web> <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-cn" uiCulture="zh-cn" /> </system.web> 顯示為 ¥3,000.10
{0:c}??? 或 string.Format("{0:C}", price); 美國貨幣樣式 <system.web> <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> </system.web> 顯示為 $3,000.10
需要實踐一下。
?
?
格式化數據和DataBinder.Eval用法范例 ?DataBinder.Eval 它帶有三個參數:數據項的命名容器、數據字段名稱和格式化字符串。 在模板列表如DataList、DataGrid、或 Repeater,命名容器總是Container.DataItem。 Page 是另一個可以被DataBinder.Eval使用的命名容器。
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式化字符串參數是可選的。如果忽略參數,DataBinder.Eval 返回對象類型的值,
//顯示二位小數 //<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>
//{0:G}代表顯示True或False
//<ItemTemplate> // <asp:Image Width="12" Height="12" Border="0" runat="server" // AlternateText='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %>' // ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "~/images/{0:G}.gif") %>' /> // </ItemTemplate>
轉換類型 Specifier Type???? Format?? Output (Passed Double 1.42)? Output (Passed Int -12400) c? Currency??????? {0:c}???? $1.42???? -$12,400 d? Decimal???????? {0:d}??? System.FormatException? -12400 e? Scientific????? {0:e}??? 1.420000e+000??? -1.240000e+004 f? Fixed point???? {0:f}? 1.42??? -12400.00 g? General???????? {0:g}? 1.42???? -12400 n? Number with commas for thousands? {0:n}? 1.42???? -12,400 r? Round trippable??? {0:r}? 1.42???? System.FormatException x? Hexadecimal??? {0:x4}? System.FormatException?? cf90
{0:d} 日期只顯示年月日 {0:yyyy-mm-dd} 按格式顯示年月日
樣式取決于 Web.config 中的設置
{0:c}? 或 {0:£0,000.00} 貨幣樣式? 標準英國貨幣樣式 <system.web> ????? <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" uiCulture="en-US" /> </system.web> 顯示為 £3,000.10
{0:c}? 或 string.Format("{0:C}", price); 中國貨幣樣式 <system.web> ????? <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-cn" uiCulture="zh-cn" /> </system.web> 顯示為 ¥3,000.10
{0:c}? 或 string.Format("{0:C}", price); 美國貨幣樣式 <system.web> ????? <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> </system.web> 顯示為 $3,000.10
-------------------------------------------------
一、DataBinder.Eval的基本格式 在綁定數據時經常會用到這個句程序:或者 今天又學到一種,而且微軟也說這種方法的效率要比以上兩種高。 很有用的,這樣可以在前臺頁面做好多事情了。 還要記住要這樣用必須要在前臺頁面導入名稱空間System.Data,否則會生成錯誤信息。 這種用法其實和是一個道理。 Text='' 這樣的方法是最快的 Text='' 也可以綁定方法,但方法要是public的 Text='' 還可以連接多個字段 關鍵是Container這個東西,它比較神秘。它的名稱空間是System.ComponentModel。對于它我還需要進一步理解。 二、DataBinder.Eval實現判斷選擇 cs里定義DGFormatSex方法 protected string DGFormatSex(string xb) { if(xb == "1") return "男"; else return "女"; } DataBinder.Eval用法范例 DataBinder.Eval用法范例 //顯示二位小數 // //{0:G}代表顯示True或False // // // //轉換類型 ((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4) {0:d} 日期只顯示年月日 {0:yyyy-mm-dd} 按格式顯示年月日 {0:c} 貨幣樣式
?
?
?
DataFormatString使用筆記
?
DataFormatString 屬性語法如下: DataFormatString=”{0:格式字符串}” 我們知道在DataFormatString 中的 {0} 表示數據本身,而在冒號后面的格式字符串代表所們希望數據顯示的格式;另外在指定的格式符號后可以指定小數所要顯示的位數。例如原來的數據為「1.56」,若格式設定為 {0:N1},則輸出為「1.5」。其常用的數值格式如下表所示: 格式字符串 輸入 結果 “{0:C}” 12345.6789 $12,345.68 “{0:C}” -12345.6789 ($12,345.68) “{0:D}” 12345 12345 “{0:D8}” 12345 00012345 “{0:E}” 12345.6789 1234568E+004 “{0:E10}” 12345.6789 1.2345678900E+004 “{0:F}” 12345.6789 12345.68 “{0:F0}” 12345.6789 12346 “{0:G}” 12345.6789 12345.6789 “{0:G7}” 123456789 1.234568E8 “{0:N}” 12345.6789 12,345.68 “{0:N4}” 123456789 123,456,789.0000 “Total: {0:C}” 12345.6789 Total: $12345.68 其常用的日期格式如下表所示: 格式 說明 輸出格式 d 精簡日期格式 MM/dd/yyyy D 詳細日期格式 dddd, MMMM dd, yyyy f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm F 完整日期時間格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss g 一般格式 (short date + short time) MM/dd/yyyy HH:mm G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss m,M 月日格式 MMMM dd s 適中日期時間格式 yyyy-MM-dd HH:mm:ss t 精簡時間格式 HH:mm T 詳細時間格式 HH:mm:ss
?
?
?
字符格式化
存儲在數據庫里的是日期時間,但在頁面中只顯示日期,之前是在查詢時就做了處理,用access里面的mid( format(renewTime,'yyyy-mm-dd'),6,5)把日期取出來再顯示。今天才看到datalist和gridview里面可以用DataFormatString來進行格式化,使數據按指定的格式顯示。
我的例子:
??????????? <asp:DataList ID="dlistNews" runat="server"? > ??????????? <ItemTemplate> ????????? <a title=<%#DataBinder.Eval(Container.DataItem,"title")%> target="_blank" href='sysbin/news/news_show.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>'> ????????? <%#GetSubString(DataBinder.Eval(Container.DataItem,"title").ToString(),24)%></a> ?????? (<%# DataBinder.Eval(Container.DataItem,"showtime")%>)
<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:d}")%> 或
<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:yyyy-MM-dd}")%> ????????????? </span> ??????????? </ItemTemplate> ??????? </asp:DataList>
如果用bind()來取值,同樣把指定格式的字符串放在后面
<ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("AddinTime", "{0:yyyy-mm-dd}") %>'></asp:Label>
</ItemTemplate> </asp:TemplateField>
控制數字:DataFormatString = "{0:F}",是默認格式,顯示兩位小數,如果需要顯示的小數位數為其他
值,DataFormatString = "{0:Fn}"即可.
具體用法:
DataFormatString="{0:格式字符串}"
在DataFormatString 中的 {0} 表示數據本身,而在冒號后面的格式字符串代表所們希望數據顯示的格式;
數字、貨幣格式: 在指定的格式符號后可以指定小數所要顯示的位數。例如原來的數據為「1.56」,若格式設定為 {0:N1},則輸
出為「1.5」。其常用的數值格式如下表所示:
格式字符串 輸入 結果 "{0:C}" 12345.6789 $12,345.68 "{0:C}" -12345.6789 ($12,345.68) "{0:D}" 12345 12345 "{0:D8}" 12345 00012345 "{0:E}" 12345.6789 1234568E+004 "{0:E10}" 12345.6789 1.2345678900E+004 "{0:F}" 12345.6789 12345.68 "{0:F0}" 12345.6789 12346 "{0:G}" 12345.6789 12345.6789 "{0:G7}" 123456789 1.234568E8 "{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000 "Total: {0:C}" 12345.6789 Total: $12345.68
常用的日期時間格式:
格式 說明 輸出格式 d 精簡日期格式 MM/dd/yyyy D 詳細日期格式 dddd, MMMM dd, yyyy f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm F 完整日期時間格式
(long date + long time) dddd, MMMM dd, yyyy HH:mm:ss g 一般格式 (short date + short time) MM/dd/yyyy HH:mm G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss m,M 月日格式 MMMM dd s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm T 詳細時間格式 HH:mm:ss
注意:如果用DataFormatString格式化GridView , 同時設置HtmlEncode = false,才能夠使DataFormatString生效. ?<asp:BoundField HeaderText="總計" DataField="Total" DataFormatString="{0:C}" HtmlEncode="False">
-----------------------------------------------------------------------------------------------------------------------------------------
DataBinder:數據綁定管理器 Eval:求值 Container:被綁定到的容器,比如GridView,DataList等 DataItem:容器的數據項,包括項、交替模板行 shipname:綁定到容器的字段(來自數據庫表字段) 給你擴展下: Eval( " ")和Bind( " ") 這兩種一個單向綁定,一個雙向綁定
bind是雙向綁定,但需數據源可更改才能用
ASP.NET 2.0改善了模板中的數據綁定操作,把v1.x中的數據綁定語法DataBinder.Eval(Container.DataItem, fieldname)簡化為Eval(fieldname)。Eval方法與DataBinder.Eval一樣可以接受一個可選的格式化字符串參數??s短的Eval語法與DataBinder.Eval的不同點在于,Eval會根據最近的容器對象(例如DataListItem)的DataItem屬性來自動地解析字段,而DataBinder.Eval需要使用參數來指定容器。由于這個原因,Eval只能在數據綁定控件的模板中使用,而不能用于Page(頁面)層。當然,ASP.NET 2.0頁面中仍然支持DataBinder.Eval,你可以在不支持簡化的Eval語法的環境中使用它。
下面的例子演示了如何使用新的簡化的Eval數據綁定語法綁定到DataList數據項模板(ItemTemplate)中的Image、Label和HyperLink控件。
<asp:DataList ID= "DataList1 " RepeatColumns= "5 " Width= "600 " runat= "server " DataSourceID= "ObjectDataSource1 "> <ItemTemplate> ? <asp:HyperLink ID= "HyperLink1 " runat= "server " NavigateUrl= '<%# Eval( "PhotoID ", "PhotoFormViewPlain.aspx?ID={0} ") %> '> ? <asp:Image ID= "Image1
轉載于:https://www.cnblogs.com/lh123/p/3979111.html
總結
以上是生活随笔為你收集整理的一些数据格式化-Eval( )和DataBinder.Eval(Container.DataItem, )的区别及用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: threejs 导入gltf模型并添加S
- 下一篇: 【训练题】航线设计 | 使用最长上升子序