2012-04-23 65 views
2

我正在使用CreateUserWizard和自定义MembershipProvider一起将用户添加到我们的数据库。目前用户已成功添加到数据库,并且正在使用CreatedUser事件来存储表单上捕获的附加信息。这工作正常;但是,我希望能够在更新期间处理任何错误情况。从CreatedUser取消CreateUserWizard事件

如果更新的附加细节失败,有没有办法重新显示带有错误的CreateUserWizard窗体?

这里是我在CreatedUser事件中的代码:

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 
    } 
} 

提前感谢

回答

1

OK,找到了一个解决办法,破解了这一个

首先,修改CreatedUser事件处理程序:

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); 
    } 
} 

接下来,我添加了一个的CustomValidator到的ContentTemplate,这样我可以显示错误消息:

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

最后,我为CreatingUser ev添加了一个新的处理程序从上下文中读取错误消息并使用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

如果您使用的是内容模板添加了ID的ErrorMessage文字控制可以显示从错误中CreateUserWizard控件。

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

感谢胡安,我已经在内容模板中定义了一个名为ErrorMessage的文字,正如预期的那样,它在创建用户时显示自定义MembershipProvider引发的任何错误。我遇到的问题是当我尝试更新CreatedUser事件中的辅助表时 – oliver 2012-04-25 20:47:46