参数:数据项的命名容器;
数据字段名;
格式字符串;
<%@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()方法绑定数据相关推荐

  1. databinder.eval绑定数据的时候显示html标签,c#教程之基于DataBinder.Eval方法的数据绑定...

    https://www.xin3721.com/eschool/ 10.2.5基于DataBinder.Eval方法的数据绑定 为将绑定的数据按指定数据类型转化为字符串,可以使用String.Form ...

  2. DataBinder.Eval方法研究

    DataBinder.Eval方法: 用途:在运行时使用反射来分析和计算对象的数据绑定表达式 重载列表如下: public static object Eval(object container,st ...

  3. 【转】DataBinder.Eval 方法

    [转自]http://www.cnblogs.com/huaxia/archive/2007/09/06/884337.html 一.DataBinder.Eval的基本格式 在绑定数据时经常会用到这 ...

  4. DataBinder.Eval数据绑定中的使用

    DataBinder.Eval (Object, String) 在运行时计算数据绑定表达式. DataBinder.Eval (Object, String, String) 在运行时计算数据绑定表 ...

  5. ASP.ENT前台更改绑定数据的日期格式

    1.Eval和Bind的区别 绑定表达式 < %# Eval("字段名") %> < %# Bind("字段名") %> 区别 1.ev ...

  6. ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)

    紧接上篇: 接下来是自定义布局的例子,我们大家先看下程序出来的效果图: 代码是: 1.     首先自定义布局,我们命名一个custom_list_layout.xml的布局文件,代码如下: < ...

  7. ASP.NET中的Eval()和DataBinder.Eval()方法

    Eval( " ")和Bind( " ") 这两种一个单向绑定,一个双向绑定 bind是双向绑定,但需数据源可更改才能用 ASP.NET 2.0改善了模板中的数 ...

  8. 一些数据格式化-Eval( )和DataBinder.Eval(Container.DataItem, )的区别及用法

      ASP.NET 2.0改善了模板中的数据绑定操作,把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fieldn ...

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

    <%# Bind("Subject") %> //绑定字段 <%# Container.DataItemIndex + 1%> //实现自动编号 <% ...

最新文章

  1. 网络工程师考试部分技术要点
  2. javax.swing super()方法
  3. SpringMVC学习(四)——Spring使用到的设计模式分析
  4. Django框架——模型(数据库操作)
  5. Java7并发编程指南——第七章:定制并发类
  6. matlab 波前像差,波前像差原理及应用
  7. 洛谷4014最大/小费用最大流
  8. python库怎么绘画_python中的turtle库(图形绘画库)
  9. python使用lxml库对解析后的DOM树形成的xpath计算得到平均值、中位数、方差
  10. 24个基本指标精粹讲解(22)--ADR
  11. AquaCrop_原理学习笔记06:土壤水分运动
  12. 简明python教程五----数据结构(下)
  13. C语言 常量和宏定义
  14. 安装使用python-pcl调用ICP算法|debug
  15. MSF图形化界面Viper(炫彩蛇)下载与使用
  16. 学习3ds max—做自行车车轮
  17. 硬件设计——有源/无源滤波
  18. 8.2. Monetary Types
  19. 晨曦记账本如何添加、登录账本以及取回密码
  20. python与ruby

热门文章

  1. Docker阿里云镜像存储服务
  2. hdu1466 递推
  3. 【RecyclerView】 十五、使用 ItemTouchHelper 实现 RecyclerView 拖动排序 ( ItemTouchHelper 简介 )
  4. 【Netty】NIO 选择器 ( Selector ) 简介
  5. 【云计算 Hadoop】Hadoop 版本 生态圈 MapReduce模型
  6. 医疗影像网络PACS系统方案
  7. 【前端词典】如何向老板解释反向代理
  8. 判断三个数最大的数 从结果出发思考问题
  9. NHibernate 状态的概念 以及 小测试
  10. 如何运维能让网站稳定高效--稳定篇