只是想分享一些我想出的速记函数,以使会话变量不再那么笨拙。 问题:
Session("NameOfSomething","Bob")
Label1.Text = Session("NameOfSomething")
Session.Remove("NameOfSomething")
解决方案:
s("NameOfSomething","Bob")
Label1.Text = s("NameOfSomething")
sr("NameOfSomething")
它是如何实现的:
Module SesVars
Public Function s(ByVal SesVarName As String, Optional ByVal SesVal As String = Nothing)
If SesVal IsNot Nothing Then System.Web.HttpContext.Current.Session("" & SesVarName & "") = SesVal
If System.Web.HttpContext.Current.Session("" & SesVarName & "") Is Nothing Then s = Nothing : Exit Function
s = System.Web.HttpContext.Current.Session("" & SesVarName & "")
End Function
Public Sub sr(ByVal SesVarName)
System.Web.HttpContext.Current.Session.Remove("" & SesVarName & "")
End Sub
End Module