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