ASP.NET创建用户控件的问题

发布于 2024-07-30 19:19:42 字数 14309 浏览 11 评论 0原文

我已经自定义了 asp.net 登录控件,它似乎可以很好地创建新帐户,但如果我复制已注册的用户 ID 或输入已使用的电子邮件,则不会显示错误消息。 这让我疯狂。 页面只是刷新而没有显示错误。

我已按照 MSDN 网站上的说明包含了 ,但什么也没有。 http://msdn.microsoft.com/en-us/library/ms178342。 ASPX

<asp:CreateUserWizard ErrorMessageStyle-BorderColor="Azure" ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/home.aspx">

                        <WizardSteps>

                            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">

                                <ContentTemplate>
                                    <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>


                                    <div class="fieldLine">
                                        <asp:Label ID="lblFirstName" runat="server" Text="First Name:" AssociatedControlID="tbxFirstName"></asp:Label>
                                        <asp:Label ID="lblLastName" runat="server" Text="Last Name:" AssociatedControlID="tbxLastName"></asp:Label>
                                    </div> 

                                    <div class="fieldLine">
                                        <asp:TextBox ID="tbxFirstName" runat="server"></asp:TextBox>
                                        <asp:TextBox ID="tbxLastName" runat="server"></asp:TextBox>
                                    </div>


                                    <asp:Label ID="lblEmail" runat="server" Text="Email:" AssociatedControlID="Email"></asp:Label>
                                    <asp:TextBox ID="Email" runat="server" CssClass="wideInput"></asp:TextBox><br />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                                                                runat="server" 
                                                                CssClass="aspValidator"
                                                                Display="Dynamic"
                                                                ControlToValidate="Email"
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                                                                    runat="server" 
                                                                    Display="Dynamic"
                                                                    CssClass="aspValidator"
                                                                    ControlToValidate="Email"
                                                                    SetFocusOnError="true"
                                                                    ValidationExpression="^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$"
                                                                    ErrorMessage="Email address not valid"></asp:RegularExpressionValidator>

                                    <asp:Label ID="lblEmailConfirm" runat="server" Text="Confirm Email Address:" AssociatedControlID="tbxEmailConfirm"></asp:Label>
                                    <asp:TextBox ID="tbxEmailConfirm" runat="server" CssClass="wideInput"></asp:TextBox><br />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
                                                                runat="server" 
                                                                CssClass="aspValidator"
                                                                Display="Dynamic"
                                                                ControlToValidate="tbxEmailConfirm"
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" 
                                                                    runat="server" 
                                                                    Display="Dynamic"
                                                                    CssClass="aspValidator"
                                                                    ControlToValidate="tbxEmailConfirm"
                                                                    SetFocusOnError="true"
                                                                    ValidationExpression="^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$"
                                                                    ErrorMessage="Email address not valid"></asp:RegularExpressionValidator>
                                    <asp:CompareValidator ID="CompareValidator1" 
                                                          runat="server" 
                                                          Display="Dynamic"
                                                          SetFocusOnError="true"
                                                          CssClass="aspValidator"
                                                          ControlToCompare="Email"
                                                          ControlToValidate="tbxEmailConfirm"
                                                          ErrorMessage="Email address' do not match"></asp:CompareValidator>

                                    <asp:Label ID="lblUsername" runat="server" Text="Username:" AssociatedControlID="UserName"></asp:Label>
                                    <asp:TextBox ID="UserName" runat="server" MaxLength="12"></asp:TextBox><br />
                                    <asp:CustomValidator ID="CustomValidatorUserName" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ValidateEmptyText="true"
                                                         ControlToValidate="UserName"
                                                         ErrorMessage="Username can be between 6 and 12 characters."
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidateUserName"></asp:CustomValidator>

                                    <div class="fieldLine">
                                        <asp:Label ID="lblPassword" runat="server" Text="Password:" AssociatedControlID="Password"></asp:Label> 
                                        <asp:Label ID="lblPasswordConfirm" runat="server" Text="Confirm Password:" AssociatedControlID="ConfirmPassword" CssClass="confirmPassword"></asp:Label> 
                                    </div>

                                    <div class="fieldLine">
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox><br />
                                        <asp:CustomValidator ID="CustomValidatorPassword" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ControlToValidate="Password"
                                                         ValidateEmptyText="true" 
                                                         ErrorMessage="Password can be between 6 and 12 characters"
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidatePassword"></asp:CustomValidator>
                                        <asp:CustomValidator ID="CustomValidatorConfirmPassword" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ControlToValidate="ConfirmPassword"
                                                         ValidateEmptyText="true" 
                                                         ErrorMessage="Password can be between 6 and 12 characters"
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidatePassword"></asp:CustomValidator>
                                          <asp:CompareValidator ID="CompareValidator2" 
                                                          runat="server" 
                                                          Enabled="false"
                                                          Display="Dynamic"
                                                          SetFocusOnError="true"
                                                          CssClass="aspValidator"
                                                          ControlToCompare="Password"
                                                          ControlToValidate="ConfirmPassword"
                                                          ErrorMessage="Passwords do not match"></asp:CompareValidator>  
                                    </div>

                                    <asp:Label ID="lblCaptch" runat="server" Text="Captcha:" AssociatedControlID="imgCaptcha"></asp:Label>
                                    <div class="borderBlue" style="width:200px;">
                                        <asp:Image ID="imgCaptcha" runat="server" ImageUrl="~/JpegImage.aspx" /><br />                                    
                                    </div>

                                    <asp:TextBox ID="tbxCaptcha" runat="server" CssClass="captchaText"></asp:TextBox>
                                    <asp:RequiredFieldValidator ControlToValidate="tbxCaptcha" 
                                                                CssClass="aspValidator" 
                                                                ID="RequiredFieldValidator3" 
                                                                runat="server" 
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:CustomValidator ID="CustomValidator1" ControlToValidate="tbxCaptcha"
                                                         runat="server" OnServerValidate="ValidateCaptcha" ErrorMessage="Captcha incorrect"></asp:CustomValidator>
                                </ContentTemplate>

                                <CustomNavigationTemplate>
                                    <div style="float:left;">
                                        <asp:Button ID="CreateUser" runat="server" Text="Register Now!" 
                                                    CausesValidation="true" CommandName="CreateUser" OnCommand="CreateUserClick" CssClass="registerButton" />
                                    </div>
                                </CustomNavigationTemplate>
                            </asp:CreateUserWizardStep>

                            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                                <ContentTemplate>
                                    <table border="0" style="font-size: 100%; font-family: Verdana" id="TABLE1" >
                                        <tr>
                                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d; height: 18px;">
                                                Complete</td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Your account has been successfully created.<br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right" colspan="2">
                                                <asp:Button ID="Button1" PostBackUrl="~/home.aspx" runat="server" Text="Button" />
                                            </td>
                                        </tr>
                                    </table>
                                </ContentTemplate>
                            </asp:CompleteWizardStep>
                        </WizardSteps>
                    </asp:CreateUserWizard>

I've customised the asp.net login control and it seems to create new accounts fine, but if I duplicate the user id thats already registered or enter an email thats already used, the error messages arn't displaying. Its driving me crazy. The page just refreshes without showing an error.

I've included the as instructed on the MSDN site but nothing. http://msdn.microsoft.com/en-us/library/ms178342.aspx

<asp:CreateUserWizard ErrorMessageStyle-BorderColor="Azure" ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/home.aspx">

                        <WizardSteps>

                            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">

                                <ContentTemplate>
                                    <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>


                                    <div class="fieldLine">
                                        <asp:Label ID="lblFirstName" runat="server" Text="First Name:" AssociatedControlID="tbxFirstName"></asp:Label>
                                        <asp:Label ID="lblLastName" runat="server" Text="Last Name:" AssociatedControlID="tbxLastName"></asp:Label>
                                    </div> 

                                    <div class="fieldLine">
                                        <asp:TextBox ID="tbxFirstName" runat="server"></asp:TextBox>
                                        <asp:TextBox ID="tbxLastName" runat="server"></asp:TextBox>
                                    </div>


                                    <asp:Label ID="lblEmail" runat="server" Text="Email:" AssociatedControlID="Email"></asp:Label>
                                    <asp:TextBox ID="Email" runat="server" CssClass="wideInput"></asp:TextBox><br />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                                                                runat="server" 
                                                                CssClass="aspValidator"
                                                                Display="Dynamic"
                                                                ControlToValidate="Email"
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                                                                    runat="server" 
                                                                    Display="Dynamic"
                                                                    CssClass="aspValidator"
                                                                    ControlToValidate="Email"
                                                                    SetFocusOnError="true"
                                                                    ValidationExpression="^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$"
                                                                    ErrorMessage="Email address not valid"></asp:RegularExpressionValidator>

                                    <asp:Label ID="lblEmailConfirm" runat="server" Text="Confirm Email Address:" AssociatedControlID="tbxEmailConfirm"></asp:Label>
                                    <asp:TextBox ID="tbxEmailConfirm" runat="server" CssClass="wideInput"></asp:TextBox><br />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
                                                                runat="server" 
                                                                CssClass="aspValidator"
                                                                Display="Dynamic"
                                                                ControlToValidate="tbxEmailConfirm"
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" 
                                                                    runat="server" 
                                                                    Display="Dynamic"
                                                                    CssClass="aspValidator"
                                                                    ControlToValidate="tbxEmailConfirm"
                                                                    SetFocusOnError="true"
                                                                    ValidationExpression="^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$"
                                                                    ErrorMessage="Email address not valid"></asp:RegularExpressionValidator>
                                    <asp:CompareValidator ID="CompareValidator1" 
                                                          runat="server" 
                                                          Display="Dynamic"
                                                          SetFocusOnError="true"
                                                          CssClass="aspValidator"
                                                          ControlToCompare="Email"
                                                          ControlToValidate="tbxEmailConfirm"
                                                          ErrorMessage="Email address' do not match"></asp:CompareValidator>

                                    <asp:Label ID="lblUsername" runat="server" Text="Username:" AssociatedControlID="UserName"></asp:Label>
                                    <asp:TextBox ID="UserName" runat="server" MaxLength="12"></asp:TextBox><br />
                                    <asp:CustomValidator ID="CustomValidatorUserName" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ValidateEmptyText="true"
                                                         ControlToValidate="UserName"
                                                         ErrorMessage="Username can be between 6 and 12 characters."
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidateUserName"></asp:CustomValidator>

                                    <div class="fieldLine">
                                        <asp:Label ID="lblPassword" runat="server" Text="Password:" AssociatedControlID="Password"></asp:Label> 
                                        <asp:Label ID="lblPasswordConfirm" runat="server" Text="Confirm Password:" AssociatedControlID="ConfirmPassword" CssClass="confirmPassword"></asp:Label> 
                                    </div>

                                    <div class="fieldLine">
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox><br />
                                        <asp:CustomValidator ID="CustomValidatorPassword" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ControlToValidate="Password"
                                                         ValidateEmptyText="true" 
                                                         ErrorMessage="Password can be between 6 and 12 characters"
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidatePassword"></asp:CustomValidator>
                                        <asp:CustomValidator ID="CustomValidatorConfirmPassword" 
                                                         runat="server" 
                                                         Display="Dynamic"
                                                         SetFocusOnError="true"
                                                         CssClass="aspValidator"
                                                         ControlToValidate="ConfirmPassword"
                                                         ValidateEmptyText="true" 
                                                         ErrorMessage="Password can be between 6 and 12 characters"
                                                         ClientValidationFunction="ValidateLength"
                                                         OnServerValidate="ValidatePassword"></asp:CustomValidator>
                                          <asp:CompareValidator ID="CompareValidator2" 
                                                          runat="server" 
                                                          Enabled="false"
                                                          Display="Dynamic"
                                                          SetFocusOnError="true"
                                                          CssClass="aspValidator"
                                                          ControlToCompare="Password"
                                                          ControlToValidate="ConfirmPassword"
                                                          ErrorMessage="Passwords do not match"></asp:CompareValidator>  
                                    </div>

                                    <asp:Label ID="lblCaptch" runat="server" Text="Captcha:" AssociatedControlID="imgCaptcha"></asp:Label>
                                    <div class="borderBlue" style="width:200px;">
                                        <asp:Image ID="imgCaptcha" runat="server" ImageUrl="~/JpegImage.aspx" /><br />                                    
                                    </div>

                                    <asp:TextBox ID="tbxCaptcha" runat="server" CssClass="captchaText"></asp:TextBox>
                                    <asp:RequiredFieldValidator ControlToValidate="tbxCaptcha" 
                                                                CssClass="aspValidator" 
                                                                ID="RequiredFieldValidator3" 
                                                                runat="server" 
                                                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    <asp:CustomValidator ID="CustomValidator1" ControlToValidate="tbxCaptcha"
                                                         runat="server" OnServerValidate="ValidateCaptcha" ErrorMessage="Captcha incorrect"></asp:CustomValidator>
                                </ContentTemplate>

                                <CustomNavigationTemplate>
                                    <div style="float:left;">
                                        <asp:Button ID="CreateUser" runat="server" Text="Register Now!" 
                                                    CausesValidation="true" CommandName="CreateUser" OnCommand="CreateUserClick" CssClass="registerButton" />
                                    </div>
                                </CustomNavigationTemplate>
                            </asp:CreateUserWizardStep>

                            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                                <ContentTemplate>
                                    <table border="0" style="font-size: 100%; font-family: Verdana" id="TABLE1" >
                                        <tr>
                                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d; height: 18px;">
                                                Complete</td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Your account has been successfully created.<br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right" colspan="2">
                                                <asp:Button ID="Button1" PostBackUrl="~/home.aspx" runat="server" Text="Button" />
                                            </td>
                                        </tr>
                                    </table>
                                </ContentTemplate>
                            </asp:CompleteWizardStep>
                        </WizardSteps>
                    </asp:CreateUserWizard>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拿命拼未来 2024-08-06 19:19:43

找到解决方案。 看来 CustomNavigationTemplate 部分中的按钮需要“MoveNext”命令参数,并且按钮 ID 必须由 StepNextButtonButton 指定。 我通过让控件在没有任何自定义的情况下呈现自身来发现这一点,然后查看其属性。

希望这对某人有帮助。

Solution found. It seems the the button in the CustomNavigationTemplate section needed a command argument of 'MoveNext' and the button ID had to by StepNextButtonButton. I found this out by letting the control render itself without any customization and then looked into its properties.

Hope this helps someone.

青萝楚歌 2024-08-06 19:19:43

在asp.net中创建一个发送邮件的函数
& 使用尝试& catch 函数用于在用户控件中获取错误
如果发现任何错误,则错误会自动发送到您的 ID,

如果您使用 gmail,请使用此函数

// 声明用于发送邮件的变量
字符串 tomail = 您的 Gmail ID
字符串主题 = 在用户控件中发现错误
string body = 用户控制

    // send a mail by gmail account


    System.Net.Mail.MailMessage MyMailMessage =
        new System.Net.Mail.MailMessage("xyz.com", tomail,

主题中的异常,body);
MyMailMessage.IsBodyHtml = false;

    //Proper Authentication Details need to be passed when sending email from gmail
    System.Net.NetworkCredential mailAuthentication = new
    System.Net.NetworkCredential("[email protected]", "password");
    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this details changes and you can
    //get it from respective server.
    System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
    //Enable SSL

    mailClient.EnableSsl = true;

    mailClient.UseDefaultCredentials = false;

    mailClient.Credentials = mailAuthentication;

    try
    {

        mailClient.Send(MyMailMessage);
    }
    catch (System.Net.Mail.SmtpException ex)
    {
        Response.Write(ex.ToString());
    }
}

create a function for sending mail in asp.net
& use try & catch function for get error in your user control
if any error found then error automatically send to your id

use this function if you use gmail

// declare variable for sending mail
string tomail = your gmail id
string subject = error found in user control
string body = exception in your user control

    // send a mail by gmail account


    System.Net.Mail.MailMessage MyMailMessage =
        new System.Net.Mail.MailMessage("xyz.com", tomail,

subject, body);
MyMailMessage.IsBodyHtml = false;

    //Proper Authentication Details need to be passed when sending email from gmail
    System.Net.NetworkCredential mailAuthentication = new
    System.Net.NetworkCredential("[email protected]", "password");
    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this details changes and you can
    //get it from respective server.
    System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
    //Enable SSL

    mailClient.EnableSsl = true;

    mailClient.UseDefaultCredentials = false;

    mailClient.Credentials = mailAuthentication;

    try
    {

        mailClient.Send(MyMailMessage);
    }
    catch (System.Net.Mail.SmtpException ex)
    {
        Response.Write(ex.ToString());
    }
}
本王不退位尔等都是臣 2024-08-06 19:19:42

在 MSDN 示例中,代码隐藏中有用于检查现有用户的代码。 您的应用程序中有类似的代码吗?

In the MSDN example there's code in the codebehind that checks for an existing user. Do you have similar code in your app?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文