在这里,我将展示如何自定义创建用户向导 & 我正在使用Microsoft Visual Studio Express 2013 For Web
在您的 register.aspx 页面中
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser" CreateUserButtonText="Submit">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep" runat="server">
<ContentTemplate>
<table>
<tr>
<td>User name:
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password:
</td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr class="form-group">
<td>E-mail:
</td>
<td >
<asp:TextBox ID="Email" runat="server" TextMode="Email"></asp:TextBox>
</td>
</tr>
<tr>
<td>First Name:
</td>
<td>
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Last Name:
</td>
<td>
<asp:TextBox ID="LastName" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Register.aspx.csCreateUserWizard1_CreatedUser
文件后面的代码使用此代码
var manager = new UserManager();
var user = new ApplicationUser() { UserName = CreateUserWizard1.UserName };
IdentityResult result = manager.Create(user, CreateUserWizard1.Password);
// Get the UserId of the just-added user
MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);
Guid newUserId = (Guid)newUser.ProviderUserKey;
//Get Profile Data Entered by user in Create User Wizard control
String FirstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
String LastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
// Insert a new record into User_Profile
// Get your Connection String from the web.config. MembershipConnectionString is the name I have in my web.config
string connectionString = ConfigurationManager.ConnectionStrings["yourConnectionstring"].ConnectionString;
string insertSql = "INSERT INTO yourDBTableName(dbTcolumn,dbTcolumn, dbTcolumn) values(@dbTcolumn, @dbTcolumn)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
IdentityHelper.SignIn(manager, user, isPersistent: false);
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@dbTcolumn", FirstName);
myCommand.Parameters.AddWithValue("@dbTcolumn", LastName);
myCommand.Parameters.AddWithValue("@dbTcolumn", newUserId);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
我不是using RequiredFieldValidator
。如果你想使用,只需使用 Register.aspx 页面下面的部分
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator4" runat="server"
ControlToValidate="FirstName" ErrorMessage="The user name field is required." ValidationGroup="CreateUserWizard1"></asp:RequiredFieldValidator>
并在您的 Register.aspx.cs 中
添加CreateUserWizard1_CreatedUser
代码
我希望这有助于您自定义创建用户向导。
谢谢if (result.Succeeded)