0

嗨,我开始使用 asp.net
开始建立一个网站,但在这里我遇到了困难,
使用了包含注册用户控件的现成主题,其中包含一个
用于接受用户名 n 密码的文本框。

我通过复制粘贴用户名和密码的代码添加了另外 3 个控件,特别是联系人号码、地址等。


但是我添加的文本框在 ASPX.CS 文件中无法访问!

我可以访问现成的控件:RegisterUser.UserName
但是当我尝试访问RegisterUser.Address它时它不可用

代码:

    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser">
        <FinishCompleteButtonStyle BorderStyle="None" />
        <FinishPreviousButtonStyle BorderStyle="None" />
        <LayoutTemplate>
            <asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder>
            <asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <ContinueButtonStyle BorderStyle="None" />
        <WizardSteps>
            <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
                <ContentTemplate>
                    <h2>
                        Create a New Account
                    </h2>
                    <p>
                        Use the form below to create a new account.
                    </p>
                    <p>
                        Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length.
                    </p>
                    <span class="failureNotification">
                        <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
                    </span>
                    <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
                         ValidationGroup="RegisterUserValidationGroup"/>
                    <div class="accountInfo">
                        <fieldset class="register">
                            <legend>Account Information</legend>
                            <p>
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                                     CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                                <asp:TextBox ID="Email" runat="server" CssClass="textEntry"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" 
                                     CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                                     CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                                <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
                                     ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" 
                                     ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                     CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator>
                            </p>
                            <p>
                                <asp:Label ID="AddressLabel" runat="server" AssociatedControlID="Address">Address:</asp:Label>
                                <asp:TextBox ID="Address" runat="server" CssClass="textEntry" ></asp:TextBox>
                                <asp:RequiredFieldValidator ID="AddressRequired" runat="server" ControlToValidate="Address" 
                                     CssClass="failureNotification" ErrorMessage="Address is required." ToolTip="Address is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="ContactLabel" runat="server" AssociatedControlID="Contact">Contact No:</asp:Label>
                                <asp:TextBox ID="Contact" runat="server" CssClass="textEntry" ></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Contact" 
                                     CssClass="failureNotification" ErrorMessage="Contact is missing." ToolTip="Contact is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                                <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Should contain 10-12 digits."></asp:CustomValidator>
                            </p>
                            <p>
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="SecurityQuestion">Security Question:</asp:Label>
                                <asp:TextBox ID="SecurityQuestion" runat="server" CssClass="textEntry" ></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="SecurityQuestion" 
                                     CssClass="failureNotification" ErrorMessage="Security Question Required" ToolTip="Security Question is crucial for your account recovery, incase you lose it!" 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                             <p>
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="SecurityAnswer">Answer:</asp:Label>
                                <asp:TextBox ID="SecurityAnswer" runat="server" CssClass="textEntry" ></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="SecurityAnswer" 
                                     CssClass="failureNotification" ErrorMessage="An Answer is required to the question!" ToolTip="Answer pertaining to the question above." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                        </fieldset>
                        <p class="submitButton">
                            <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" EnableTheming=true 
                                 ValidationGroup="RegisterUserValidationGroup"/>
                        </p>
                    </div>
                </ContentTemplate>
                <CustomNavigationTemplate>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>
        </WizardSteps>
        <StartNextButtonStyle BorderStyle="None" />
        <StepNextButtonStyle BorderStyle="None" />
        <StepPreviousButtonStyle BorderStyle="None" />
    </asp:CreateUserWizard>

很惊讶!!请帮助,

4

1 回答 1

0

您可以通过几个步骤实现这一目标。

  1. 在表 aspnet_Users 表中创建一个新字段 Mobile。

  2. 将您的 createUserWizard 转换为模板。

我正在删除大部分代码以保持简短和美观。请注意移动文件如何包含在向导中。代码:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 

        CreateUserButtonText="Sign Up" 
        oncreateduser="CreateUserWizard1_CreatedUser" >
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" Title="CreateUser" runat="server">
                <ContentTemplate>
                    <table border="0" style="background:gray" id="TABLE2">
                        <tr>
                            <td align="center" colspan="2">
                                Sign Up for Your New Account</td>
                        </tr>
                        <tr>
                            <td align="right" class="style4">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User 
                                Name:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server">   </asp:TextBox>
                            </td>
                        </tr>

您的手机号码:输入手机号码

  1. 在代码后面添加命名空间 System.Data.SqlClient ;

并在 page_load 事件下方使用此代码

代码:

 protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{

    if (CreateUserWizard1.ActiveStep.Title == "CreateUser")
    {
        TextBox mobile = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Mob");
        string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        string insertSql = "INSERT INTO aspnet_Users(Mobile) VALUES(@AddMob)";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
            myCommand.Parameters.AddWithValue("@AddMob", mobile.Text);
            myCommand.ExecuteNonQuery();

            myConnection.Close();
        }
    }
}

希望这可以帮助...

于 2013-10-17T07:20:01.140 回答