您好,登錄后才能下訂單哦!
這篇文章主要介紹“ASP.NET Eval如何進(jìn)行數(shù)據(jù)綁定”,在日常操作中,相信很多人在ASP.NET Eval如何進(jìn)行數(shù)據(jù)綁定問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ASP.NET Eval如何進(jìn)行數(shù)據(jù)綁定”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
假設(shè)你已經(jīng)了解ASP.NET Eval 1.1的數(shù)據(jù)綁定(特別是Container這個(gè)局部變量)的機(jī)制,這里主要分析ASP.NET Eval 2.0數(shù)據(jù)綁定做了那些改進(jìn).
ASP.NET Eval 2.0 的數(shù)據(jù)綁定函數(shù)Eval()簡化掉了ASP.NET Eval 1.1神秘的Container.DataItem,比如數(shù)據(jù)綁定表達(dá)式:
復(fù)制代碼 代碼如下:
<%# (Container.DataItem as DataRowView)["ProductName"].ToString() %>
ASP.NET Eval 1.1簡化為:(去掉了類型指定, Eval通過反射實(shí)現(xiàn),本文不再闡述)
復(fù)制代碼 代碼如下:
<%# DataBinder.Eval(Container.DataItem, "ProductName").ToString() %>
ASP.NET Eval 2.0又簡化為,去掉了Container局部變量:
<%# Eval("ProductName") %>
那么,Page.Eval()又是如何知道"ProductName"是那個(gè)數(shù)據(jù)的屬性呢,即Container.DataItem真的消失了嗎?
ASP.NET Eval()是Page的父類TemplateControl的方法
TemplateControl.Eval()可以自動計(jì)算出Container, 機(jī)制就是從一個(gè)dataBindingContext:Stack堆棧來獲取.
1. 建立DataItem Container 棧:
在Control.DataBind()中,建立,這樣可以保證子控件的DataItem Container始終在棧頂.
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
public class Page
{
public object GetDataItem()
{
...
return this._dataBindingContext.Peek(); <-- 讀取堆棧頂部的DataItem Container,就是正在綁定的DataItem Container
}
}
3. TemplateControl.Eval()
復(fù)制代碼 代碼如下:
public class TemplateControl
{
protected string Eval (string expression, string format)
{
return DataBinder.Eval (Page.GetDataItem(), expression, format);
}
}
結(jié)論:
從上面看出Page.Eval()在計(jì)算的時(shí)候還是引用了Container.DataItem,只不過這個(gè)DataItem通過DataItem Container堆棧自動計(jì)算出來的.我認(rèn)為Page.Eval()看似把問題簡化了,其實(shí)把問題搞得更加神秘.
到此,關(guān)于“ASP.NET Eval如何進(jìn)行數(shù)據(jù)綁定”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。