DataBinder.Eval()方法绑定数据
生活随笔
收集整理的這篇文章主要介紹了
DataBinder.Eval()方法绑定数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參數(shù):數(shù)據(jù)項(xiàng)的命名容器; 數(shù)據(jù)字段名; 格式字符串; <%@DataBinder.Eval(Container.DataItem,"max_lvl","{0:c}") %> 備注:c 代表以人民幣符號顯示; p 代表以百分號符號顯示; 示例: 使用DataBinder.Eval()方法進(jìn)行數(shù)據(jù)綁定 ---導(dǎo)入命名空間 <%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object semder, EventArgs e)
{
// 創(chuàng)建數(shù)據(jù)庫連接字符串及SqlDataAdapter對象
string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];
SqlConnection myConnection = new SqlConnection(ConnStr);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
// 生成DataSet對象并填充數(shù)據(jù)
DataSet ds = new DataSet();
myCommand.Fill(ds, "Titles");
// 將Repeater控件進(jìn)行數(shù)據(jù)綁定
MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;
MyRepeater.DataBind();
}
</script>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<table width="100%" style="font: 8pt verdana">
<tr style="background-color:DFA894">
<th>Title</th>
<th>Title ID</th>
<th>Type</th>
<th>Publisher ID</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "title_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "type") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</ASP:Repeater>
</body>
</html> ------------------------------------------------------------------------------------------- <%# Bind("Subject") %> //綁定字段
<%# Container.DataItemIndex + 1%> //實(shí)現(xiàn)自動編號
<%# 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", "~/p_w_picpaths/{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。
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object semder, EventArgs e)
{
// 創(chuàng)建數(shù)據(jù)庫連接字符串及SqlDataAdapter對象
string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];
SqlConnection myConnection = new SqlConnection(ConnStr);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
// 生成DataSet對象并填充數(shù)據(jù)
DataSet ds = new DataSet();
myCommand.Fill(ds, "Titles");
// 將Repeater控件進(jìn)行數(shù)據(jù)綁定
MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;
MyRepeater.DataBind();
}
</script>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<table width="100%" style="font: 8pt verdana">
<tr style="background-color:DFA894">
<th>Title</th>
<th>Title ID</th>
<th>Type</th>
<th>Publisher ID</th>
<th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "title_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "type") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</ASP:Repeater>
</body>
</html> ------------------------------------------------------------------------------------------- <%# Bind("Subject") %> //綁定字段
<%# Container.DataItemIndex + 1%> //實(shí)現(xiàn)自動編號
<%# 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", "~/p_w_picpaths/{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。
轉(zhuǎn)載于:https://blog.51cto.com/hndtraveller/162327
總結(jié)
以上是生活随笔為你收集整理的DataBinder.Eval()方法绑定数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux rpm 包安装不了,解决方法
- 下一篇: CIW《操作系统安全》目录