<%# 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. C/C++ 获取汉字拼音首字母

    #include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...

  2. LightOj1028 - Trailing Zeroes (I)---求因子个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1028 题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它 ...

  3. Introduction to Project Management(I)

    Project management in the modern sense began in the early 1950s, although it has its roots further b ...

  4. Tomcat 处理请求时的中文乱码问题

    利用Tomcat8作为服务器,采用servlet接收前端请求后进行处理的过程中,前台请求中有中文时,中文信息变成了乱码. 经过调试和查阅,发现Tomcat在处理get请求和post请求是有区别的.参照 ...

  5. .NET(c#)new关键字的三种用法

    前几天去家公司面试,有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法:用于在泛型声明中约束可能用作类型参数的参数的类型,这是 ...

  6. asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法

    最近项目在 IE 11 下<asp:LinkButton> 点击出现 “__doPostBack”未定义”,经过一番google,终于知道了原因:ASP.NET 可能无法辨识出一些浏览器的 ...

  7. C语言下WebService的使用方式

    用gSoap工具:      1.在dos环境中到gSoap工具对应的目录gsoap_2.8.18\gsoap-2.8\gsoap\bin\win32路径下,执行wsdl2h -c -o *.h ht ...

  8. ubantu install chrome

    ubantu apt-get installt -y openssh-server sudo apt-get -f install libappindicator1 libindicator7dpkg ...

  9. myeclipse中的web项目导入到eclipse中注意事项,项目部署到tomcat后无法访问jsp文件

    打开eclipse,点击空白处,右键可以看到import>general>existing projects into workspace>next>选择你的myeclipse ...

  10. Vue.2.0.5-单文件组件

    介绍 在很多Vue项目中,我们使用 Vue.component 来定义全局组件,紧接着用new Vue({ el: '#container '}) 在每个页面内指定一个容器元素. 这种方案在只是使用 ...