2012-08-27 117 views
1

我想使用密碼恢復使用vb。我在網上找到了c#代碼,並試圖將它轉換爲vb。我得到「期望聲明結束」錯誤。任何人都可以看到這個問題嗎?vb.net轉換C#轉換爲文字

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 

回答

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

可以只是

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

如果你願意,你也可以用命令類型(對象,類型)或DirectCast(對象,類型) - 示例:

Dim errorLiteral As Literal = CType(PasswordRecovery1.UserNameTemplateContainer.FindControl("FailureText"), Literal) 
+0

謝謝,工作。 – user1202606