0

I would like add error message to page validation in order to display it on the validation summary but how?

here is my goal on the code behind file;

try {
    DateTime.Parse(txtBirthDate.Text);
}
catch (Exception err) {

    //Adding error message to page validation 
    return;
}

NOTE: Of course I used validations on my aspx page and I both validate them on either client side and server side but I waana know if this thing is possible. It can be done on the controller on ASP.NET MVC with ModelState.AddModelStateError() Method. I am exactly looking for something like that.

3 Answers 3

1

If you are using the ASP.NET validation controls, then they will automatically set the validation summary to the error message that you specify.

If you want to validate on the server side, then use CustomValidator. If your server-side validator indicates a problem, then the validation summary should still display the error message specified in the validation control.

Sign up to request clarification or add additional context in comments.

Comments

0

Do not use a try catch block to validate your date input. If you really want to do this in code behind, use tryparse instead, in combination with a CustomValidator:

  void ServerValidation (object source, ServerValidateEventArgs arguments)
  {
        arguments.IsValid = (DateTime.TryParse(arguments.value, out dateValue));
  }

Comments

0

See also On postback, how can I add a error message to validation summary?

You can create a new Validator control and add it to the page to create a new validation error message, if a CustomValidator doesn't fit what you are trying to do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.