zoukankan      html  css  js  c++  java
  • ASP 2.0 数据绑定函数Eval()的机制

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

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

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

     ASP 1.1简化为:(去掉了类型指定, Eval通过反射实现,本文不再阐述)
     <%# DataBinder.Eval(Container.DataItem, "ProductName").ToString() %>      

     ASP 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()看似把问题简化了,其实把问题搞得更加神秘。

  • 相关阅读:
    http://msdn.microsoft.com/zhcn/library/cc838145(VS.95).aspx
    去除HTML标签2005SQL写法
    UML中符号的意义(转)
    删除DataTable中重复的记录
    Matlab R2010在centost下的安装
    Eclipse 编译 Android工程时,提示该错误 :Error generating final archive: Debug certificate expired on xxxxxx(日期) 解决办法
    centos上安装opencv库
    windows下eclipse远程连接Hadoop集群进行开发
    centos6 上用eclipse调试hadoop程序报org.apache.hadoop.io.compress.SnappyCodec not found错误解决方法
    cocoa设计模式笔记
  • 原文地址:https://www.cnblogs.com/ma/p/429807.html
Copyright © 2011-2022 走看看