views:

285

answers:

2

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

A: 

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.

brumScouse
Thanks for your input! In my case it would be too much work for to little effect. I'm only using this on one place on my web and it's not a big website. I think I'm looking for something along the lines of a quick fix that is easy to implement. Then on the other hand, aren't we always looking for that. ;-)
tkahn
+1  A: 

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.

tkahn