<%# 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

1,

<asp:HyperLink runat="server" ID="hlUserName" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>' NavigateUrl='<%# string.Format("UserView.aspx?uId={0}",Eval("UserId")) %>' />

2,可以调用cs 中的内部函数和自定义函数:

(1):

<HyerLink runat="server" ID="hlUserName" Text='<%# DateTime.Now.ToString()%>' />

(2):

cs 中:

protected string test(string s)

{return s.ToUpper();}

aspx中:

<HyperLink runat="server" ID="hlUserName" Text='<%# test("aaa")%>'  />

3,不放在控件中,直接调用函数

<%= DateTime.Now.ToLongDateString()%>

格式化日期:

<%# Eval("finishtime","{0:yyyy-MM-dd}") %>
   在绑定数据时经常会用到这个句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>        微软这种方法的效率更高,但我不常用,我习惯了上一种。

<%# ((DataRowView)Container.DataItem)["xxxx"]%> 用这种方法首先要在前台页面导入名称空间System.Data,否则会生成错误信息。

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

DataBinder.Eval()可以梆定方法,Text='<%# PBnumber(DataBinder.Eval(Container.DataItem,"photoBookID")) %>后台代码:
        protected string PBnumber(object PBid)
        {

string str = "[ " + Convert.ToString(PBc.GetInPbkPnum((int)PBid)) + " ] 张";

return str;
        }

DataBinder.Eval还可以判断选择,如以性别为例:

<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<%# DGFormatSex(Convert.ToString(DataBinder.Eval(Container.DataItem,"xb"))) %>
</ItemTemplate>
</asp:TemplateColumn>

cs里定义DGFormatSex方法
protected string DGFormatSex(string xb)
{
if(xb == "1")
return "男";
else
return "女";
}

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} 货币样式

 本文假设你已经了解ASP.NET 1.1的数据绑定(特别是Container这个局部变量)的机制,这里主要分析ASP 2.0数据绑定做了那些改进。

  ASP.NET 2.0 的数据绑定函数Eval()简化掉了ASP 1.1神秘的Container.DataItem,比如数据绑定表达式:

<%# (Container.DataItem as DataRowView)["ProductName"].ToString() %>

  ASP.NET 1.1简化为:(去掉了类型指定, Eval通过反射实现,本文不再阐述)

<%# DataBinder.Eval(Container.DataItem, "ProductName").ToString() %>

  ASP.NET 2.0又简化为,去掉了Container局部变量:

<%# Eval("ProductName") %>

  那么,Page.Eval()又是如何知道"ProductName"是那个数据的属性呢,即Container.DataItem真的消失了吗?

  Eval()是Page的父类TemplateControl的方法

  TemplateControl.Eval()可以自动计算出Container, 机制就是从一个dataBindingContext:Stack堆栈来获取。

  1. 建立DataItem Container 栈:

  在Control.DataBind()中,建立,这样可以保证子控件的DataItem Container始终在栈顶。

public class Control
{
 protected virtual void DataBind(bool raiseOnDataBinding)
 {
  bool foundDataItem = false;
  if (this.IsBindingContainer)
  {
   object o = DataBinder.GetDataItem(this, out foundDataItem);
   if (foundDataItem)
    Page.PushDataItemContext(o); <-- 将DataItem压入堆栈
  }
  try
  {
   if (raiseOnDataBinding)
    OnDataBinding(EventArgs.Empty);

   DataBindChildren(); <-- 绑定子控件
  }
  finally
  {
   if (foundDataItem)
    Page.PopDataItemContext(); <-- 将DataItem弹出堆栈
  }
 }
}

  2. 获取DataItem Container

public class Page

 public object GetDataItem()
 {
  ...
  return this._dataBindingContext.Peek(); <-- 读取堆栈顶部的DataItem Container,就是正在绑定的DataItem    Container
 }
}

  3. TemplateControl.Eval()

public class TemplateControl
{
 protected string Eval (string expression, string format)
 {
  return DataBinder.Eval (Page.GetDataItem(), expression, format); 
 }
}

  

结论:

  从上面看出Page.Eval()在计算的时候还是引用了Container.DataItem,只不过这个DataItem通过DataItem Container堆栈自动计算出来的。我认为Page.Eval()看似把问题简化了,其实把问题搞得更加神秘。

C# eval()函数浅谈的更多相关文章

  1. Javascript-回调函数浅谈

    回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定 ...

  2. 继承虚函数浅谈 c++ 类,继承类,有虚函数的类,虚拟继承的类的内存布局,使用vs2010打印布局结果。

    本文笔者在青岛逛街的时候突然想到的...最近就有想写几篇关于继承虚函数的笔记,所以回家到之后就奋笔疾书的写出来发布了 应用sizeof函数求类巨细这个问题在很多面试,口试题中很轻易考,而涉及到类的时候 ...

  3. 从SG函数浅谈解决博弈问题的通法

    基于笔者之前对于几种二元零和博弈游戏的介绍,这里将其思想进行简单的提炼,并引出解决这类二元零和博弈游戏的强大工具——SG函数. 其实对于博弈游戏如Bash.Nim等基本类型,异或一些比较高级的棋类游戏 ...

  4. Sql Server存储过程和函数浅谈

    今天给大家总结一下sql server中的存储过程和函数.本人是小白,里面内容比较初级,大神不喜勿喷 自行飘过就是.. 首先给大家简单列出sql server中的流控制语句,后面会用到的^_^ sql ...

  5. 从Java继承类的重名static函数浅谈解析调用与分派

    在java中,static成员函数是否可以被重写呢? 结论是,你可以在子类中重写一个static函数,但是这个函数并不能像正常的非static函数那样运行. 也就是说,虽然你可以定义一个重写函数,但是 ...

  6. Hash函数浅谈

    Hash函数是指把一个大范围映射到一个小范围.把大范围映射到一个小范围的目的往往是为了节省空间,使得数据容易保存. 除此以外,Hash函数往往应用于查找上.所以,在考虑使用Hash函数之前,需要明白它 ...

  7. 浅谈JavaScript eval() 函数

    用js的人都应该知道eval()函数吧,虽然该函数用的极少,但它却功能强大,那么问题来了,为什么不常用呢?原因很简单,因为eval()函数是动态的执行其中的字符串,里面有可能是脚本,那么这样的话就有可 ...

  8. 开发技术--浅谈Python函数

    开发|浅谈Python函数 函数在实际使用中有很多不一样的小九九,我将从最基础的函数内容,延伸出函数的高级用法.此文非科普片~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点 ...

  9. 浅谈javascript函数节流

    浅谈javascript函数节流 什么是函数节流? 函数节流简单的来说就是不想让该函数在很短的时间内连续被调用,比如我们最常见的是窗口缩放的时候,经常会执行一些其他的操作函数,比如发一个ajax请求等 ...

随机推荐

  1. 读书笔记——《图解TCP/IP》(3/4)

    经典摘抄 第五章 IP协议相关技术 1.DNS可以将网址自动转换为具体的IP地址. 2.主机识别码的识别方式:为每台计算机赋以唯一的主机名,在进行网络通信时,可以直接使用主机名称而无需输入一大长串的I ...

  2. VS下如何调试多线程

    四步即可 1.打开多线程窗口,找到当前线程 此时,出现窗口如下: 2.右击任意位置,选中全部线程 3.停止全部线程 此时,线程状态如下: 4.单独启动当前线程:先单击当前线程,在点击启动按钮,如下红色 ...

  3. 利用堆排序找出数组中前n大的元素

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <time.h> ...

  4. SQL Server 取日期时只要年月或年月日

    select CONVERT(varchar(7) ,getdate(), 120) as 'Date' from 表名;--只取年月且日期格式为 xxxx-xx select CONVERT(var ...

  5. 微信支付开发(11) Native支付

    关键字:微信公众平台 微信支付 Native原生支付作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/wxpay-native.html 由于微信支付接口更新,本 ...

  6. 端口转发后执行putty连接------------------》VirtualBox+ubuntu_server

    login as: fleam fleam@127.0.0.1's password: Welcome to Ubuntu LTS (GNU/Linux --generic i686) * Docum ...

  7. 利用JBoss漏洞拿webshell方法

    JBoss是一个大型应用平台,普通用户很难接触到.越是难以接触到的东西越觉得高深,借用北京公交司机李素丽的一句话“用力只能干出称职,用心才能干出优秀”,在安全上也是如此,虽然JBoss平台很难掌握,但 ...

  8. MPI简介

    什么是MPI: MPI是一个库,而不是一门语言.但是按照并行语言的分类,可以把FORTRAN+MPI或者C+MPI看作是一种在原来串行语言基础上扩展后得到的并行语言.MPI库可以被FORTRAN77/ ...

  9. The L1 Median (Weber 1909)

    The L1 Median (Weber 1909) 链接网址 Derived from a transportation cost minimization problem, the L1 medi ...

  10. [GeoServer]Openlayers简单调用

    Openlayers Demo: <html> <head> <title>OpenLayers Example</title> <script ...