我一直在按照教程在此处的“创建用户向导”中创建其他字段:
http://www.4guysfromrolla.com/articles/070506-1.aspx#postadlink
我收到一个错误,即 Object reference not set to an instance on an object on my line DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
我的 ASP 代码是这样的:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreateduser="CreateUserWizard1_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table>
<tr>
<td class="text-center" colspan="2">
Sign Up for Your New Account
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
<td class="text-center">
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
</td>
<td>
<asp:TextBox ID="Email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="ManagerFirstNameLabel" runat="server" AssociatedControlID="ManagerFirstName">First Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="ManagerFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="ManagerFirstNameRequired" runat="server" ControlToValidate="ManagerFirstName"
ErrorMessage="First Name is required." ToolTip="First Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="ManagerSurnameLabel" runat="server" AssociatedControlID="ManagerSurname">Surname:</asp:Label>
</td>
<td>
<asp:TextBox ID="ManagerSurname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="ManagerSurnamRequired" runat="server" ControlToValidate="ManagerSurname"
ErrorMessage="Surname is required." ToolTip="Surname is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="TeamNameLabel" runat="server" AssociatedControlID="TeamName">Team Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="TeamName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="TeamNameRequired" runat="server" ControlToValidate="TeamName"
ErrorMessage="Team Name is required." ToolTip="Team Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="TeamStadiumLabel" runat="server" AssociatedControlID="TeamStadium">Team Stadium Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="TeamStadium" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="TeamStadiumRequired" runat="server" ControlToValidate="TeamStadium"
ErrorMessage="Team Stadium Name is required." ToolTip="Team Stadium Name is required."
ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="RecieveReminderLabel" runat="server" AssociatedControlID="RecieveReminder">Recieve Reminder Email:</asp:Label>
</td>
<td>
<asp:CheckBox ID="RecieveReminder" runat="server" />
</td>
</tr>
<tr>
<td class="text-right">
<asp:Label ID="RecieveSummaryLabel" runat="server" AssociatedControlID="RecieveSummary">Recieve Summary Email:</asp:Label>
</td>
<td>
<asp:CheckBox ID="RecieveSummary" runat="server" />
</td>
</tr>
<tr>
<td class="text-center" colspan="2" style="color: Red;">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td class="text-center" colspan="2" style="color: Red;">
<asp:Literal ID="Literal1" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
<asp:SqlDataSource ID="InsertAdditionalUserDetails" runat="server" ConnectionString="<%$ ConnectionStrings:RaiseFantasyLeagueConnectionString %>"
InsertCommand="INSERT INTO [UserDetails] ([UserId], [ManagerFirstName], [ManagerSurname], [TeamName], [TeamStadium], [RecieveReminder], [RecieveSummary]) VALUES (@UserId, @ManagerFirstName, @ManagerSurname, @TeamName, @TeamStadium, @RecieveReminder, @RecieveSummary)" ProviderName="System.Data.SqlClient">
<InsertParameters>
<asp:ControlParameter Name="ManagerFirstName" Type="String" ControlID="ManagerFirstName" PropertyName="Text" />
<asp:ControlParameter Name="ManagerSurname" Type="String" ControlID="ManagerSurname" PropertyName="Text" />
<asp:ControlParameter Name="TeamName" Type="String" ControlID="TeamName" PropertyName="Text" />
<asp:ControlParameter Name="TeamStadium" Type="String" ControlID="TeamStadium" PropertyName="Text" />
<asp:ControlParameter Name= "RecieveReminder" Type="Boolean" ControlID="RecieveReminder" PropertyName="Checked" />
<asp:ControlParameter Name= "RecieveSummary" Type="Boolean" ControlID="RecieveSummary" PropertyName="Checked" />
</InsertParameters>
</asp:SqlDataSource>
而C#代码是这样的
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
TextBox UserNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
SqlDataSource DataSource = (SqlDataSource)CreateUserWizardStep1.ContentTemplateContainer.FindControl("InsertAdditionalUserDetails");
MembershipUser User = Membership.GetUser(UserNameTextBox.Text);
if (User != null)
{
object UserGUID = User.ProviderUserKey;
DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
DataSource.Insert();
}
}
任何帮助,将不胜感激 !