我有一个奇怪的问题。我使用了很多会话变量,所以每次页面进行回发时我都不必来回传递它。多年来我一直这样做,所以我完全失去了。
我在App_Code文件夹中有一个名为SessionHandler.vb的文件,其中包含以下代码:
Imports Microsoft.VisualBasic
Public Class SessionHandler
Private Shared _chgLinePkNum As String = "0"
Private Shared _chgStudentIDPk As String = "0"
Public Shared Property chgLinePkNum() As String
Get
' Check for null first
If (HttpContext.Current.Session(SessionHandler._chgLinePkNum) Is Nothing) Then
' Return an empty string if session variable is null.
Return "Nothing"
Else
Return HttpContext.Current.Session(SessionHandler._chgLinePkNum).ToString()
End If
End Get
Set(ByVal value As String)
If (value Is Nothing) Or (value = "") Then
HttpContext.Current.Session(SessionHandler._chgLinePkNum) = "Nothing"
Else
HttpContext.Current.Session(SessionHandler._chgLinePkNum) = value
End If
End Set
End Property
Public Shared Property chgStudentIDPk() As String
Get
' Check for null first
If (HttpContext.Current.Session(SessionHandler._chgStudentIDPk) Is Nothing) Then
' Return an empty string if session variable is null.
Return "Nothing"
Else
Return HttpContext.Current.Session(SessionHandler._chgStudentIDPk).ToString()
End If
End Get
Set(ByVal value As String)
If (value Is Nothing) Or (value = "") Then
HttpContext.Current.Session(SessionHandler._chgStudentIDPk) = "Nothing"
Else
HttpContext.Current.Session(SessionHandler._chgStudentIDPk) = value
End If
End Set
End Property
足够简单......然后,在我的代码中,我通过SessionHandler.chgLinePkNum引用属性。这段代码的LineItemNumber = 1,StudentID = [实际ID号]。
If IsParking And checkbox.Checked = True Then
SessionHandler.chgLinePkNum = LineItemNumber
SessionHandler.chgStudentIDPk = StudentID
peParkingRegistration.Show()
End If
当第一行运行时,chgLinePkNum按预期设置为1。由于一些奇怪的原因,它还将chgStudentIDPk设置为1.当运行下一行时,它将chgStudentIDPk设置为正确的StudentID号。问题是,它还将chgLinePkNum设置为StudentID号。
我在调试器中逐行运行它,每个属性集函数只在调用它时运行。我只是想不通“HttpContext.Current.Session(SessionHandler._chgLinePkNum)= value”是如何设置chgStudentIDPk的值,反之亦然。
答案 0 :(得分:1)
与这些具有完全相同的值有什么关系吗?
Private Shared _chgLinePkNum As String = "0"
Private Shared _chgStudentIDPk As String = "0"