简体   繁体   English

ASP.NET登录控件 - 我可以将FailureText作为项添加到ValidationSummary中吗?

[英]ASP.NET login control - can I add the FailureText as an item in a ValidationSummary?

I'm currently working with the ASP.NET login control. 我目前正在使用ASP.NET登录控件。 I can set a custom failure text and I can add a literal on the page where the failure text is displayed if the login fails. 我可以设置自定义失败文本,如果登录失败,我可以在显示失败文本的页面上添加文字。 I also have a validation summary on the page in which I collect all errors that can occur (for the moment it just validates that the user has entered a login name and a password. 我还在页面上有一个验证摘要,我收集了所有可能发生的错误(目前它只是验证用户输入了登录名和密码。

It would be really nice if I could add the failure text of the login control as an item in the validation summary, but I'm not sure if this is even possible? 如果我可以将登录控件的失败文本添加为​​验证摘要中的项目,那将是非常好的,但我不确定这是否可能?

Hoping that the massive brainpower of stackoverflow can give me some pointers? 希望stackoverflow的大量智能可以给我一些指示?

Thanks! 谢谢!

/Thomas Kahn PS. / Thomas Kahn PS。 I'm coding in C#. 我在C#中编码。

I found a solution that works! 我找到了一个有效的解决方案!

On the page I add a CustomValidator, like this: 在页面上我添加了一个CustomValidator,如下所示:

<asp:CustomValidator id="vldLoginFailed" runat="server" ErrorMessage="Login failed. Please check your username and password." ValidationGroup="loginControl" Visible="false"></asp:CustomValidator>

I also have a ValidationSummary that looks like this: 我还有一个ValidationSummary,如下所示:

<asp:ValidationSummary id="ValidationSummary" ValidationGroup="loginControl" runat="server" DisplayMode="BulletList" CssClass="validationSummary" HeaderText="Please check the following"></asp:ValidationSummary>

On my login control I add a method to OnLoginError, so it looks like this: 在我的登录控件上,我向OnLoginError添加了一个方法,所以它看起来像这样:

<asp:Login ID="loginControl" runat="server" VisibleWhenLoggedIn="false" OnLoginError="loginControl_LoginError">

In my codebehind I create a method that is triggered when there's a login error and it looks like this: 在我的代码隐藏中,我创建了一个在出现登录错误时触发的方法,它看起来像这样:

protected void loginControl_LoginError(object sender, EventArgs e)
{
    CustomValidator vldLoginFailed = (CustomValidator)loginControl.FindControl("vldLoginFailed");
    vldLoginFailed.IsValid = false;
}

So when there's a login error the method loginControl_LoginError will be called. 因此,当出现登录错误时,将调用loginControl_LoginError方法。 It finds the CustomValidator and sets IsValid to false. 它找到CustomValidator并将IsValid设置为false。 Since the CustomValidator belongs to the validation group "loginControl" its error message will be displayed in the ValidationSummary. 由于CustomValidator属于验证组“loginControl”,因此其错误消息将显示在ValidationSummary中。

Potentially, 潜在的,

You could override the Render method in ValidationSummary control checking for the errors reported by the login control. 您可以在ValidationSummary控件中覆盖Render方法,检查登录控件报告的错误。 Im not sure whether how the errors are reported, but if a validation control is utilised inspecting the Page.Validators collection will get you the information you need to update the output of the Validation Summary. 我不确定是否报告错误,但如果使用验证控件,检查Page.Validators集合将获得更新验证摘要输出所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM