這是我剛碰到的一件奇怪的事情。爲什麼我的ApplicationCache傳回一個引用而不是一個值?
我有一個Web應用程序,在ApplicationCache中存儲了一個小DataTable,以減少查詢的數量,因爲數據是不經常更改的查找表。
我在給定的頁面內訪問這個DataTable兩次。一旦該數據到下拉列表綁定在我的Page_Load方法:
dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.Sort = "LongDescription ASC"
ddlDeptDivAccount.DataSource = dtDeptDivAct.DefaultView
ddlDeptDivAccount.DataTextField = "LongDescription"
ddlDeptDivAccount.DataValueField = "Id"
ddlDeptDivAccount.DataBind()
...而一旦從表中檢索附加數據時,在我的ddlDeptDivAct_SelectedIndexChanged事件被選擇的指標:
Dim dtDeptDivAct As DeptDivActDataTable
If ddlDeptDivAccount.SelectedIndex > 0 Then
dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.RowFilter = "Id = " & ddlDeptDivAccount.SelectedValue
txtAddFundingDept.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Department.ToString.PadLeft(2, Char.Parse("0"))
txtAddFundingDiv.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Division.ToString.PadLeft(2, Char.Parse("0"))
txtAddFundingAct.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Activity.ToString.PadLeft(3, Char.Parse("0"))
Else
txtAddFundingDept.Text = ""
txtAddFundingDiv.Text = ""
txtAddFundingAct.Text = ""
End If
注意:GetAllDeptDivActCodes()方法是一個從ApplicationCache對象返回表的簡單方法。
該網頁正常工作。我可以選擇我的值並將正確的值插入TextBox。但是,當我轉到其他頁面並返回到此頁面時。我的下拉列表中只有1個值可供選擇。
當我拉起調試器時,我注意到在返回到網頁時,GetAllDeptDivActCodes方法從緩存中返回DataTable時,DefaultView RowFilter屬性仍然應用於導致問題的DataTable。
我已經解決了這個問題,現在只需在SelectedIndexChanged事件中完成處理後重置DefaultView RowFilter,但爲什麼應用程序在我期待時返回了應用程序緩存中對DataTable的引用對象的單獨副本(或值)?
有用的信息!!!這樣做可以讓Web應用程序的效率更高,因爲我正在拉取一些緩存的用戶詳細信息或查找代碼,因此不會將它複製到任何地方都是相當不錯的。謝謝!!! – 2008-12-29 15:45:45