ASP 2.0 資料繫結函數Eval()的機制

來源:互聯網
上載者:User
函數|資料   本文假設你已經瞭解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()看似把問題簡化了,其實把問題搞得更加神秘。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.