【问题标题】:Cancel CreateUserWizard from CreatedUser Event从 CreatedUser 事件中取消 CreateUserWizard
【发布时间】:2012-04-23 09:20:56
【问题描述】:

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

如果更新附加详细信息失败,是否有办法重新显示带有错误的 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
    }
}

在此先感谢

【问题讨论】:

    标签: c# asp.net .net createuserwizard


    【解决方案1】:

    如果您使用内容模板添加 ID 为 ErrorMessage 的文字控件,则可以显示来自 CreateUserWizard 控件的错误。

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

    【讨论】:

    • 感谢 Juan,我已经在内容模板中定义了一个名为 ErrorMessage 的字面量,并且正如预期的那样,它确实显示了自定义 MembershipProvider 在创建用户时引发的任何错误。我遇到的问题是当我尝试从 CreatedUser 事件中更新辅助表时
    【解决方案2】:

    好的,为这个找到了解决方法

    一、修改后的 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);
        }
    }
    

    接下来,我在 ContentTemplate 中添加了一个 CustomValidator,以便我可以显示错误消息:

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

    最后,我为 CreatingUser 事件添加了一个新的处理程序,以从 Context 中读取错误消息并使用 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,最重要的是,用户不会在重试之前再次重新输入所有详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2021-04-03
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多