日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

DataBinder.Eval()方法绑定数据

發布時間:2025/6/15 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataBinder.Eval()方法绑定数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參數:數據項的命名容器; 數據字段名; 格式字符串; <%@DataBinder.Eval(Container.DataItem,"max_lvl","{0:c}") %> 備注:c 代表以人民幣符號顯示; p 代表以百分號符號顯示; 示例: 使用DataBinder.Eval()方法進行數據綁定 ---導入命名空間 <%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
  
  void Page_Load(Object semder, EventArgs e)
  {
 // 創建數據庫連接字符串及SqlDataAdapter對象
 string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];
    SqlConnection myConnection = new SqlConnection(ConnStr);
    SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
 // 生成DataSet對象并填充數據
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "Titles");
 // 將Repeater控件進行數據綁定
    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%> //實現自動編號
<%# 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", "~/p_w_picpaths/{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(Container.DataItem,"Name")和Container.DataItem("Name")有什么區別?
DataBinder是System.Web里面的一個靜態類,它提供了Eval方法用于簡化數據綁定表達式的編寫,但是它使用的方式是通過Reflection等開銷比較大的方法來達到易用性,因此其性能并不是最好的。而Container則根本不是任何一個靜態的對象或方法,它是ASP.NET頁面編譯器在數據綁定事件處理程序內部聲明的局部變量,其類型是可以進行數據綁定的控件的數據容器類型(如在Repeater內部的數據綁定容器叫RepeaterItem),在這些容器類中基本都有DataItem屬性,因此你可以寫Container.DataItem,這個屬性返回的是你正在被綁定的數據源中的那個數據項。如果你的數據源是DataTable,則這個數據項的類型實際是DataRowView。

轉載于:https://blog.51cto.com/hndtraveller/162327

總結

以上是生活随笔為你收集整理的DataBinder.Eval()方法绑定数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。