0

这是我刚刚遇到的一件奇怪的事情。

我有一个 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 对象返回表的简单方法。

The web page works fine. I can select my value and the proper values are insterted into the TextBox. However, when I go to a different page and come back to this page. My drop down list only has 1 value available for selection.

When I pulled up the debugger, I noticed that upon returning to the web page, when the GetAllDeptDivActCodes method returns the DataTable from the cache, the DefaultView RowFilter property was still applied to the DataTable, which was causing the problem.

I have fixed the issue for now by simply resetting the the DefaultView RowFilter once processing is done in the SelectedIndexChanged event, but why is the Application returning what appears to be a reference to the DataTable in the application cache when I was expecting a seperate copy (or value) of the object?

4

1 回答 1

2

This is by design. Whenever you store an object in the Application State or Session State you are returned the actual object (or as you put it a reference to the object) when you access it. By design .NET objects are almost always passed by reference unless you specify otherwise. Forexample when passing objects to Functions they are passed by reference.

于 2008-12-27T02:06:14.820 回答