将变量从代码的一部分传递给另一部分

时间:2014-03-01 22:15:17

标签: asp.net vb.net

如何设置变量,然后在代码的另一部分中检索该变量?我试图在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

1 个答案:

答案 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

Properties Overview