<%# 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. Java回调实现

    一.回调的形式 1. C.C++和Pascal允许将函数指针作为参数传递给其它函数.JavaScript,Python,和PHP允许简单的将函数名作为参数传递. 2. .NET Framework的语 ...

  2. 【Java 基础篇】【第三课】表达式、控制结构

    这两天再看敏捷开发流程,我这个算是敏捷博客吗? 哈哈o(∩_∩)o package a.b; public class Three { static void Expression() { Syste ...

  3. 20145211 《Java程序设计》实验报告二:Java面向对象程序设计

    实验要求 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验内容 单元测试 面向对象三要素 设计模式初步 练习 实 ...

  4. ArcGIS Engine开发之旅01---产品组成、逻辑体系结构

    原文:ArcGIS Engine开发之旅01---产品组成.逻辑体系结构 ArcGIS Engine 由两个产品组成:  面向开发人员的软件开发包(ArcGIS Engine Developer k ...

  5. ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题

    原文:ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题 问题:如果在地图上加载成百上千工程点时,会密密麻麻,外观不是很好看,怎么破? 解决方法: ...

  6. [LeetCode]题解(python):059-Spiral Matrix II

    题目来源 https://leetcode.com/problems/spiral-matrix-ii/ Given an integer n, generate a square matrix fi ...

  7. (leetcode)Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  8. Chip Factory---hdu5536(异或值最大,01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...

  9. 安装Sql server 2008时出现sql server 2005 express tools failed 怎么办?

    提示错误:Sql2005SsmsExpressFacet 检查是否安装了 SQL Server 2005 Express 工具. 失败,已安装 SQL Server 2005 Express 工具.若 ...

  10. scrollba美化

    1.overflow内容溢出时的设置(设定被设定对象是否显示滚动条)    overflow-x水平方向内容溢出时的设置    overflow-y垂直方向内容溢出时的设置    以上三个属性设置的值 ...