国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET 數據綁定函數Eval()的機制

2019-11-17 04:07:53
字體:
來源:轉載
供稿:網友
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()看似把問題簡化了,其實把問題搞得更加神秘。

================================================================================

可以這樣說,絕大多數的大型網站都會用到eval()函數,雖然一直在用,但其具體含義及使用還真沒特別去注意過,搜索了下也沒有什么明確的解釋。

  翻譯過來的中文釋義大體如下:

  C# eval ()函數,是一個用來求值的解析表達式,支持布爾型(Boolean),二進制方式,算法,一元,所屬,

  成員(例如:object.property 對象屬性),索引值(數組索引);條件函數,也支持變量賦值。

  其有點在于:

  無需運行時編譯;

  無需使用JScript.NET;

  無需使用數據表。

  英文如下:

  C# eval function

  parse expression_r and evaluate it. Supports Boolean, Bitwise, Arithmetic, Unary, Paren, Member (such as object.property ), Indexer (such as array[index]) and ConditionalIf(?:), also supports Variable assign.

  you can use it like evaluate one statement C# source code.

  not use runtime Compiler.

  not use JScript.NET.

  not use DataTable
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福海县| 屯留县| 深州市| 永城市| 浙江省| 侯马市| 繁峙县| 淮阳县| 濮阳市| 徐汇区| 呼和浩特市| 图片| 洛川县| 安图县| 五莲县| 保康县| 湾仔区| 桂东县| 长沙市| 无棣县| 治多县| 汶上县| 富顺县| 江源县| 许昌市| 合山市| 通渭县| 阳原县| 石河子市| 句容市| 长宁区| 绥芬河市| 凤台县| 房产| 临洮县| 赣榆县| 肇东市| 沈丘县| 文昌市| 夏津县| 榕江县|