【发布时间】:2014-10-29 15:11:00
【问题描述】:
我有以下代码。在页面加载期间,我从数据库中获取客户对象。之后,当我尝试以不同的方法访问同一个对象时,该对象显示为空。假设 Student 对象具有 firstName、lastName 等属性。
Public class Test
Public oStudent as Student
Public Sub Page_Load(ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.Load
oStudent = getStudent(22) 'This is just a sample. This is not my actual database.
End Sub
Public Sub Update(ByVal sender as Object, ByVal e as System.EventArgs) Handles crtlStudent.Update
Update(oStudent)'This one updates makes a database call to update the studnet
End Sub
End class
当页面加载时,学生从数据库中正确返回。但是,当我使用更新方法时,oStudent 对象变为空/空。这是页面生命周期的工作方式吗?如果是,我需要将 oStudent 存储在会话中或缓存它吗?有没有其他方法可以防止 oStudent 变为 null 其他使用会话变量或缓存它?
【问题讨论】:
-
Web 是无状态的。对该主题进行一些研究,这已经讨论了太多次了,我的口味已经……您可以考虑切换到 MVC,只是为了了解实际发生的情况。
-
在这种情况下,我只是“作弊”并使用表单中的隐藏元素来保存值,这样它在回发期间保持在视图状态中。回发完成后,变量总是会丢失其值。