如何设置变量,然后在代码的另一部分中检索该变量?我试图在LoginUser_Authenticate部分设置变量,然后检查LoginUser_LoginError部分中的变量。
Protected Sub LoginUser_Authenticate(sender As Object, e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles LoginUser.Authenticate
If (Membership.ValidateUser(UserName.Text, Password.Text)) And (CurrentUser IsNot Nothing) Then
// set variable
else
// set variable
End If
End Sub
---------------------------------------------------------------------------------
Protected Sub LoginUser_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoginError
LoginUser.FailureText = "Invalid Username or Password - Please Try Again"
Dim CurrentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)
If (CurrentUser IsNot Nothing) And ***VARIABLE FROM ABOVE*** Then
If (CurrentUser.IsLockedOut = True) Then
LoginUser.FailureText = "Your account has been locked - Contact the system administrator"
ElseIf (CurrentUser.IsApproved = False) Then
LoginUser.FailureText = "Your account is disabled - Contact the system administrator"
End If
End If
End Sub
答案 0 :(得分:0)
使用属性或字段,例如使用Boolean
变量:
Private Property IsAuthorized As Boolean
Protected Sub LoginUser_Authenticate(sender As Object, e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles LoginUser.Authenticate
If Membership.ValidateUser(UserName.Text, Password.Text) AndAlso CurrentUser IsNot Nothing Then
IsAuthorized = True
Else
IsAuthorized = False
End If
End Sub
Protected Sub LoginUser_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoginError
' ... '
If IsAuthorized Then
' ... '
End If
End Sub