I'm currently working with the ASP.NET login control. 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.
我目前正在使用ASP.NET登录控件。我可以设置自定义失败文本,如果登录失败,我可以在显示失败文本的页面上添加文字。我还在页面上有一个验证摘要,我收集了所有可能发生的错误(目前它只是验证用户输入了登录名和密码。
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 * can give me some pointers?
希望*的大量智能可以给我一些指示?
Thanks!
/Thomas Kahn PS. I'm coding in C#.
/ Thomas Kahn PS。我用C#编码。
2 个解决方案
#1
10
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. It finds the CustomValidator and sets IsValid to false. Since the CustomValidator belongs to the validation group "loginControl" its error message will be displayed in the ValidationSummary.
因此,当出现登录错误时,将调用loginControl_LoginError方法。它找到CustomValidator并将IsValid设置为false。由于CustomValidator属于验证组“loginControl”,因此其错误消息将显示在ValidationSummary中。
#2
0
Potentially,
You could override the Render method in ValidationSummary control checking for the errors reported by the login control. 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.
您可以在ValidationSummary控件中覆盖Render方法,检查登录控件报告的错误。我不确定是否报告错误,但如果使用验证控件,检查Page.Validators集合将获得更新验证摘要输出所需的信息。
#1
10
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. It finds the CustomValidator and sets IsValid to false. Since the CustomValidator belongs to the validation group "loginControl" its error message will be displayed in the ValidationSummary.
因此,当出现登录错误时,将调用loginControl_LoginError方法。它找到CustomValidator并将IsValid设置为false。由于CustomValidator属于验证组“loginControl”,因此其错误消息将显示在ValidationSummary中。
#2
0
Potentially,
You could override the Render method in ValidationSummary control checking for the errors reported by the login control. 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.
您可以在ValidationSummary控件中覆盖Render方法,检查登录控件报告的错误。我不确定是否报告错误,但如果使用验证控件,检查Page.Validators集合将获得更新验证摘要输出所需的信息。