2012-04-23 1 views
2

사용자 정의 MembershipProvider와 함께 CreateUserWizard를 사용하여 데이터베이스에 사용자를 추가하고 있습니다. 현재 사용자가 데이터베이스에 성공적으로 추가되었으며 CreatedUser 이벤트를 사용하여 양식에 캡처 된 추가 정보를 저장하고 있습니다. 이것은 잘 작동합니다. 그러나 업데이트 도중 어떤 오류 조건도 처리 할 수 ​​있기를 원합니다.CreatedUser 이벤트에서 CreateUserWizard 취소

추가 정보 업데이트가 실패하면 오류가있는 CreateUserWizard 양식을 다시 표시하는 방법이 있습니까? 사전에

protected void RegisterUserWizard_CreatedUser(object sender, EventArgs e) 
{ 
    try 
    { 
     // Try to update the customer table with the additional information 
     using (OrderEntities entities = new OrderEntities()) 
     { 
      // Read in all the values from the form 
      TextBox custTitle = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerTitle"); 
      TextBox custName = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerName"); 
      TextBox custSurname = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerSurname"); 
      TextBox custAddress1 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine1"); 
      TextBox custAddress2 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine2"); 
      TextBox custAddress3 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine3"); 
      TextBox custCity = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCity"); 
      TextBox custCounty = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCounty"); 
      TextBox custPostcode = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerPostcode"); 
      DropDownList custCountry = (DropDownList)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCountry"); 

      Customer custInfo = entities.Customers.Where(c => c.UserName == RegisterUserWizard.UserName).FirstOrDefault(); 

      if (custInfo != null) 
      { 
       custInfo.Email = RegisterUserWizard.Email; 
       custInfo.Password = RegisterUserWizard.Password; 
       custInfo.Title = custTitle.Text; 
       custInfo.Firstname = custName.Text; 
       custInfo.Surname = custSurname.Text; 
       custInfo.AddressLine1 = custAddress1.Text; 
       custInfo.AddressLine2 = custAddress2.Text; 
       custInfo.AddressLine3 = custAddress3.Text; 
       custInfo.City = custCity.Text; 
       custInfo.County = custCounty.Text; 
       custInfo.Postcode = custPostcode.Text; 
       custInfo.CountryID = custCountry.SelectedValue; 
       custInfo.CreatedDate = DateTime.Now; 

       entities.SaveChanges(); 

       FormsAuthentication.SetAuthCookie(RegisterUserWizard.UserName, false); 

       // Redirect user back to calling page 
       string continueUrl = RegisterUserWizard.ContinueDestinationPageUrl; 
       if (String.IsNullOrEmpty(continueUrl)) 
       { 
        continueUrl = "~/"; 
       } 
       Response.Redirect(continueUrl); 

      } 
      else 
      { 
       // Redisplay CreateUserWizard showing error message 
      } 
     } 
    } 
    catch 
    { 
     // Redisplay CreateUserWizard showing error message 
    } 
} 

많은 감사는 콘텐츠 템플릿을 사용하여 ID ErrorMessage가와 리터럴 컨트롤을 추가 할 경우

답변

1

OK하기, 이것

우선, 변성 CreatedUser 이벤트 핸들러에 대한 해결 방법이 해킹 발견 I는 표시 할 수

protected void RegisterUserWizard_CreatedUser(object sender, EventArgs e) 
{ 
    try 
    { 
     // Try to update the customer table with the additional information 
     using (OrderEntities entities = new OrderEntities()) 
     { 
      // Read in all the values from the form 
      TextBox custTitle = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerTitle"); 
      TextBox custName = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerName"); 
      TextBox custSurname = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerSurname"); 
      TextBox custAddress1 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine1"); 
      TextBox custAddress2 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine2"); 
      TextBox custAddress3 = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerAddressLine3"); 
      TextBox custCity = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCity"); 
      TextBox custCounty = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCounty"); 
      TextBox custPostcode = (TextBox)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerPostcode"); 
      DropDownList custCountry = (DropDownList)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomerCountry"); 

      Customer custInfo = entities.Customers.Where(c => c.UserName == RegisterUserWizard.UserName).FirstOrDefault(); 

      if (custInfo != null) 
      { 
       custInfo.Email = RegisterUserWizard.Email; 
       custInfo.Password = RegisterUserWizard.Password; 
       custInfo.Title = custTitle.Text; 
       custInfo.Firstname = custName.Text; 
       custInfo.Surname = custSurname.Text; 
       custInfo.AddressLine1 = custAddress1.Text; 
       custInfo.AddressLine2 = custAddress2.Text; 
       custInfo.AddressLine3 = custAddress3.Text; 
       custInfo.City = custCity.Text; 
       custInfo.County = custCounty.Text; 
       custInfo.Postcode = custPostcode.Text; 
       custInfo.CountryID = custCountry.SelectedValue; 
       custInfo.CreatedDate = DateTime.Now; 

       entities.SaveChanges(); 

       FormsAuthentication.SetAuthCookie(RegisterUserWizard.UserName, false); 

       // Redirect user back to calling page 
       string continueUrl = RegisterUserWizard.ContinueDestinationPageUrl; 
       if (String.IsNullOrEmpty(continueUrl)) 
       { 
        continueUrl = "~/"; 
       } 
       Response.Redirect(continueUrl); 

      } 
      else 
      { 
       // Throw an Exception so that we redisplay CreateUserWizard showing error message 
       throw new Exception("An error occurred updating account details, please try again"); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     // Delete the incomplete user from the membership to avoid duplicate UserName errors if the user tries again 
     Membership.DeleteUser(RegisterUserWizard.UserName); 

     // Store the error message in the Context and transfer back to the page preserving the form 
     Context.Items.Add("ErrorMessage", ex.Message); 
     Server.Transfer(Request.Url.PathAndQuery, true); 
    } 
} 

다음에, I는 ContentTemplate에 직접 CustomValidator 추가 오류 메시지 :

<asp:CustomValidator ID="CustomValidator" runat="server" ValidationGroup="RegisterUserValidationGroup" /> 

마지막으로 CreatingUser ev ent에서 오류 메시지를 읽고 CustomValidator를 사용하여 표시합니다.

void RegisterUserWizard_CreatingUser(object sender, LoginCancelEventArgs e) 
{ 
    // If we have received an error message in the Context then cancel the CreatingUser and display the message 
    if (Context.Items["ErrorMessage"] != null) 
    { 
     CustomValidator custValidator = (CustomValidator)RegisterUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("CustomValidator"); 
     custValidator.ErrorMessage = Context.Items["ErrorMessage"].ToString(); 
     custValidator.IsValid = false; 
     e.Cancel = true; 
    } 
} 

이제 오류가 발생하면 오류 메시지가 나타납니다. 친숙한 메시지를 표시하고 MembershipUser를 정리할 수 있으며 가장 중요한 것은 사용자가 다시 시도하기 전에 세부 정보를 모두 다시 입력하지 않는 것입니다.

1

당신이에서 오류를 표시 할 수 있습니다 : 여기

내가 CreatedUser 이벤트가 코드입니다 CreateUserWizard 컨트롤

<asp:Literal runat="server" EnableViewState="false" ID="ErrorMessage"></asp:Literal> 
+0

감사합니다. Juan, 콘텐츠 템플릿에 이미 정의 된 ErrorMessage라는 리터럴이 있으며 예상대로 사용자 생성시 사용자 지정 MembershipProvider에서 발생한 오류를 표시합니다. 내가 가진 문제는 CreatedUser 이벤트 내에서 보조 테이블을 업데이트하려고 할 때입니다. – oliver