日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[ASP.NET]DataBinder.Eval用法,文本绑定

發(fā)布時(shí)間:2024/1/1 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ASP.NET]DataBinder.Eval用法,文本绑定 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<%# Bind("Subject") %> //綁定字段
<%# Container.DataItemIndex + 1%> //實(shí)現(xiàn)自動(dòng)編號(hào)
<%# 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}") %>
格式化字符串參數(shù)是可選的。如果忽略參數(shù),DataBinder.Eval 返回對象類型的值,

//顯示二位小數(shù)
<%# 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>
//轉(zhuǎn)換類型
((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 中的設(shè)置

{0:c}?? 或 {0:£0,000.00} 貨幣樣式?? 標(biāo)準(zhǔn)英國貨幣樣式
<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(Container.DataItem,"Name")和Container.DataItem("Name")有什么區(qū)別?
DataBinder是System.Web里面的一個(gè)靜態(tài)類,它提供了Eval方法用于簡化數(shù)據(jù)綁定表達(dá)式的編寫,但是它使用的方式 是通過Reflection等開銷比較大的方法來達(dá)到易用性,因此其性能并不是最好的。而Container則根本不是任何一個(gè)靜態(tài)的對象或方法,它是 ASP.NET頁面編譯器在數(shù)據(jù)綁定事件處理程序內(nèi)部聲明的局部變量,其類型是可以進(jìn)行數(shù)據(jù)綁定的控件的數(shù)據(jù)容器類型(如在Repeater內(nèi)部的數(shù)據(jù)綁 定容器叫RepeaterItem),在這些容器類中基本都有DataItem屬性,因此你可以寫Container.DataItem,這個(gè)屬性返回的 是你正在被綁定的數(shù)據(jù)源中的那個(gè)數(shù)據(jù)項(xiàng)。如果你的數(shù)據(jù)源是DataTable,則這個(gè)數(shù)據(jù)項(xiàng)的類型實(shí)際是DataRowView。



用DataBinder.eval 綁定不必關(guān)心數(shù)據(jù)來源(Dataread或dataset)。不必關(guān)心數(shù)據(jù)的類型eval會(huì)把這個(gè)數(shù)據(jù)對象轉(zhuǎn)換為一個(gè)字符串。在底層綁定做了很多工作,使 用了反射性能。正因?yàn)槭褂梅奖懔?#xff0c;但卻影響了數(shù)據(jù)性能。當(dāng)于dataset綁定時(shí),DataItem其實(shí)式一個(gè)DataRowView(如果綁定的是一個(gè) 數(shù)據(jù)讀取器(dataread)它就是一個(gè)IdataRecord。)因此直接轉(zhuǎn)換成DataRowView的話,將會(huì)給性能帶來很大提升。
??   使用時(shí)注意:1.注意字段名的大小寫(要特別注意)。如果和查詢的不一致,在某些情況下會(huì)導(dǎo)致比<%# DataBinder.Eval(Container.DataItem, "字段名") %>還要慢。2.如果想進(jìn)一步提高速度,可采用<%# ((System.Data.DataRowView)Container.DataItem)[0] %>的方法。不過其可讀性不高。

//以DataSet做數(shù)據(jù)源時(shí)

//正常情況(C#)

<%# ((System.Data.DataRowView)Container.DataItem)["Title"] %>


//綁定日期字段格式字符串

<%# Convert.ToDateTime(((System.Data.DataRowView)Container.DataItem)

[
"FbTime"]).ToString("yyyy年MM月dd日") %>

<%# DataBinder.Eval(Container, "DataItem.FbTime","{0:yyyy年MM月dd日}") %>


//字符串綁定超過指定長度截?cái)?/span>

<%# DataBinder.Eval(Container.DataItem, "Text").ToString().Trim().Length>7?

DataBinder.Eval(Container.DataItem,
"Text").ToString().Trim().Substring

(
0,7):DataBinder.Eval(Container.DataItem, "Text").ToString().Trim() %>


//價(jià)格格試化為2位小數(shù)

<%# Convert.ToDecimal(((System.Data.DataRowView)Container.DataItem) ["Price"]).ToString("F2") %>

<%# DataBinder.Eval(Container, "DataItem.Price","{0:F2}") %>
'VB.net中價(jià)格格式化后判斷
<%# IIf(DataBinder.Eval(Container.DataItem, "price","{0:F2}")="0.00","",DataBinder.Eval(Container.DataItem, "price","{0:F2}")+"") %>

'Vb.net常規(guī)
<%# Ctype(Container.DataItem,DataRowView).Row("Title") %>

靈活的運(yùn)用數(shù)據(jù)綁定操作
??????? 綁定到簡單屬性:<%#UserName%>
??????? 綁定到集合:<asp:ListBox id="ListBox1" datasource='<%# myArray%>' runat="server">
??????? 綁定到表達(dá)式:<%#(class1.property1.ToString() + "," + class1.property2.ToString())%>
??????? 綁定到方法返回值:<%# GetSafestring(str) %>
??????? 綁定到Hashtable:<%# ((DictionaryEntry)Container.DataItem).Key%>
??????? 綁定到ArrayList:<%#Container.DataItem %>

??????? 若數(shù)組里里放的是對象則可能要進(jìn)行必要的轉(zhuǎn)換后再綁定如:
??????? <%#((對象類型)Container.DataItem).屬性%>

??????? 綁定到DataView,DataTable,DataSet:
??????? <%#((DataRowView)Container.DataItem)["字段名"]%>或
??????? <%#((DataRowView)Container.DataItem).Rows[0]["字段名"]%>
??????? 要格式化則:
??????? <%#string.Format("格式",((DataRowView)Container.DataItem)["字段名"])%>
??????? <%#DataBinder.Eval(Container.DataItem,"字段名","格式")%>

??????? 綁定到DataReader:
??????? <%#((IDataReader)Container.DataItem).字段名%>

??????? 當(dāng)然為了方便一般使用最多的就是DataBinder類的Eval方法了.不過這樣對于同時(shí)要綁定大量的數(shù)據(jù)效率要低一些

在綁定數(shù)據(jù)時(shí)經(jīng)常會(huì)用到這個(gè)句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>

今天又學(xué)到一種,而且微軟也說這種方法的效率要比以上兩種高。

<%# ((DataRowView)Container.DataItem)["xxxx"]%>

很有用的,這樣可以在前臺(tái)頁面做好多事情了。

還要記住要這樣用必須要在前臺(tái)頁面導(dǎo)入名稱空間System.Data,否則會(huì)生成錯(cuò)誤信息。

<%@ Import namespace="System.Data" %>

這種用法其實(shí)和<%# ((DictionaryEntry)Container.DataItem).Key%>是一個(gè)道理。

綁定到DataSet、DataTable時(shí):

<%#((System.Data.DataRowView)Container.DataItem)["字段名"]%>
<%#((System.Data.DataRowView)Container.DataItem)[索引]%>

綁定到DataReader時(shí):
<%#((System.Data.Common.DbDataRecord)Container.DataItem)[索引]%>
<%#((System.Data.Common.DbDataRecord)Container.DataItem)["字段名"]%>

關(guān)鍵是Container這個(gè)東西,它比較神秘。它的名稱空間是System.ComponentModel。對于它我還需要進(jìn)一步理解。??

初學(xué).NET,現(xiàn)在在看DataGrid控件,在ItemTemplate顯示數(shù)據(jù)時(shí),
DataBinder.Eval(Container.DataItem,"Name")和Container.DataItem("Name")有什么區(qū)別?

DataBinder是System.Web里面的一個(gè)靜態(tài)類,它提供了Eval方法用于簡化數(shù)據(jù)綁定表達(dá)式的編寫,但是它使用的方式是通過 Reflection等開銷比較大的方法來達(dá)到易用性,因此其性能并不是最好的。而Container則根本不是任何一個(gè)靜態(tài)的對象或方法,它是 ASP.NET頁面編譯器在數(shù)據(jù)綁定事件處理程序內(nèi)部聲明的局部變量,其類型是可以進(jìn)行數(shù)據(jù)綁定的控件的數(shù)據(jù)容器類型(如在Repeater內(nèi)部的數(shù)據(jù)綁 定容器叫RepeaterItem),在這些容器類中基本都有DataItem屬性,因此你可以寫Container.DataItem,這個(gè)屬性返回的 是你正在被綁定的數(shù)據(jù)源中的那個(gè)數(shù)據(jù)項(xiàng)。如果你的數(shù)據(jù)源是DataTable,則這個(gè)數(shù)據(jù)項(xiàng)的類型實(shí)際是DataRowView。

轉(zhuǎn)載于:https://www.cnblogs.com/lel1447/archive/2008/09/20/1294643.html

總結(jié)

以上是生活随笔為你收集整理的[ASP.NET]DataBinder.Eval用法,文本绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。