开发者

Adding error to page validation on ASP.NET Web Forms

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.


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.


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));
  }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜