Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

Thanks!

/Thomas Kahn PS. I'm coding in C#.

like image 412
tkahn Avatar asked Apr 08 '10 08:04

tkahn


1 Answers

I found a solution that works!

On the page I add a CustomValidator, like this:

<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:

<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:

<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.

like image 133
tkahn Avatar answered Sep 27 '22 22:09

tkahn