首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CreatedUser事件中取消CreateUserWizard

从CreatedUser事件中取消CreateUserWizard
EN

Stack Overflow用户
提问于 2012-04-23 17:20:56
回答 2查看 1.5K关注 0票数 2

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

有没有办法在更新附加详细信息失败时重新显示带有错误的CreateUserWizard表单?

下面是我在CreatedUser事件中的代码:

代码语言:javascript
复制
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
    }
}

非常感谢你提前

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-26 06:56:57

好的,我找到了解决这个问题的办法

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

代码语言:javascript
复制
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,以便可以显示错误消息:

代码语言:javascript
复制
<asp:CustomValidator ID="CustomValidator" runat="server" ValidationGroup="RegisterUserValidationGroup" />

最后,我为CreatingUser事件添加了一个新的处理程序,以便从上下文中读取错误消息并使用CustomValidator显示它:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-04-25 00:21:35

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

代码语言:javascript
复制
<asp:Literal runat="server" EnableViewState="false" ID="ErrorMessage"></asp:Literal>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10277812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档