1

我正在尝试使用 vb 进行密码恢复。我在网上找到了 c# 代码,并试图将其转换为 vb。我收到“预期语句结束”错误。任何人都可以看到这个问题吗?

c#代码:

protected void validateUserEmail(object sender, LoginCancelEventArgs e)
{
    TextBox EmailAddressTB = 
        ((TextBox)PWRecovery.UserNameTemplateContainer.FindControl("EmailAddressTB"));

Literal ErrorLiteral = 
  ((Literal)PWRecovery.UserNameTemplateContainer.FindControl("ErrorLiteral"));

MembershipUser mu = Membership.GetUser(PWRecovery.UserName);

if (mu != null) // The username exists
    {
        if (mu.Email.Equals(EmailAddressTB.Text)) // Their email matches
        {
            ProfileCommon newProfile = Profile.GetProfile(PWRecovery.UserName);
            HttpCookie appCookie = new HttpCookie("usernameCookie");
            appCookie.Value = newProfile.FullName;
            appCookie.Expires = DateTime.Now.AddMinutes(3);
            Response.Cookies.Add(appCookie);
        }
        else
        {
            e.Cancel = true;
            ErrorLiteral.Text = "Your username and password do not match";
        }
    }
    else
    {
        e.Cancel = true;
        ErrorLiteral.Text = "No such user found.";
    }

}

VB代码:

Protected Sub SubmitButton_Click(sender As Object, e As System.EventArgs)

    Dim user As MembershipUser = Membership.GetUser(PasswordRecovery1.UserName)

    Dim errorLiteral As Literal = (Literal)PasswordRecovery1.UserNameTemplateContainer.FindControl("FailureText")

    If (user IsNot Nothing) Then
        Dim password As String = user.GetPassword()

        EmailPassword(user.Email, password, user.ToString())
    Else
        errorLiteral.Text = "No such user found."
    End If

End Sub
4

1 回答 1

2
Dim errorLiteral As Literal = (Literal)PasswordRecovery1.UserNameTemplateContainer.FindControl("FailureText")

可以只是

Dim errorLiteral As Literal = PasswordRecovery1.UserNameTemplateContainer.FindControl("FailureText")

如果您愿意,还可以使用 Ctype(object, type) 或 DirectCast(object, type) - 例如:

Dim errorLiteral As Literal = CType(PasswordRecovery1.UserNameTemplateContainer.FindControl("FailureText"), Literal)
于 2012-08-27T21:54:09.840 回答