7

I am pretty new to ASP.NET and C# I have spent the day learning the basics of the ASP.NET Membership provider I have built all my validator but are getting stuck at outputting my error message on the page.

private void LogCreateUserError(MembershipCreateStatus status, string username)
{
    string reasonText = status.ToString();

    switch (status)
    {
        case MembershipCreateStatus.DuplicateEmail:
        case MembershipCreateStatus.DuplicateProviderUserKey:
        case MembershipCreateStatus.DuplicateUserName:

            reasonText = "The user details you entered are already registered.";
            break;

        case MembershipCreateStatus.InvalidAnswer:
        case MembershipCreateStatus.InvalidEmail:
        case MembershipCreateStatus.InvalidProviderUserKey:
        case MembershipCreateStatus.InvalidQuestion:
        case MembershipCreateStatus.InvalidUserName:
        case MembershipCreateStatus.InvalidPassword:

            reasonText = string.Format("The {0} provided was invalid.", status.ToString().Substring(7));
            break;
        default:
            reasonText = "Due to an unknown problem, we were not able to register you at this time";
            break;

    }

   //CODE TO WRITE reasonText TO THE HTML PAGE ??

}

What is the best way to output the varible result onto the page as I have relied upon the built in ASP:Validators until now.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Yardstermister
  • 2,041
  • 3
  • 15
  • 12
  • Is this using ASP.NET MVC or WebForms? Also, do you have any specific controls on the page that you would like the status to be shown in? – Tomas Aschan May 13 '10 at 22:52
  • ASP.NET MVC, I have not put any controls in place for it as I am a little unsure what controls it could write to. Would be appropriate ? or am i drifting in the wrong direction. – Yardstermister May 13 '10 at 22:57
  • What class does that private method belong to - the controller? Could you post the code for the controller action that is calling it? – Charlino May 13 '10 at 23:04
  • Ps. With ASP.NET MVC, using anything with `runat="server"` is drifting completely down the wrong direction. – Charlino May 13 '10 at 23:09

3 Answers3

10

MVC

See for a good example...

ASP.NET MVC Html.ValidationSummary(true) does not display model errors

Basically, you need to propagate the error message and also that fact that there is an error to your view from your controller. ModelStateDictionary.AddModelError() will take care of both of these tasks for you.

You can then use ValidationExtensions.ValidationSummary() to display.

Webforms

You don't have to use a validator for this. Most people don't. A simple styled DIV should work well.

eg.

<div id="errorMessageDiv" runat="server"></div>

Notice the runat parameter.

Now in your code-behind you can try

errorMessageDiv.innerHTML = "some error message";

If you really want to use a validator checkout...

http://weblogs.asp.net/ashicmahtab/archive/2008/12/12/putting-messages-into-a-validationsummary-control-from-code.aspx

Basically you set the ErrorMessage and isValid parameters to the related validator in the code behind. The related ValidationSummary should display the error message.

TylerH
  • 20,799
  • 66
  • 75
  • 101
kervin
  • 11,672
  • 5
  • 42
  • 59
0

You can use validation summary or you can use a label control to display the error message

Bala
  • 1,386
  • 1
  • 14
  • 27
-1

If using WebForms you might use a Label control and set the '.Text' property of it with your result. Or a Panel control. Or a UserControl specifically for outputting error messages (this is what I do) that you can add to your MasterPage.

CarmineSantini
  • 294
  • 3
  • 6