Dynamically Add Error Message To Validation Summary
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more add error to validationsummary from code behind about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users mvc add error to validationsummary Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping asp.net add error to validationsummary each other. Join them; it only takes a minute: Sign up Adding error message to validation summary from code behind up vote 5 down vote favorite I am doing something like below on a web forms application; protected validationsummary add message programmatically void button_transfer_search_Click(object sender, EventArgs e) { Page.Validate("val1"); if (!Page.IsValid && int.Parse(txtArrivalDateTrf.Text) + 5 < 10) { return; } also, I have following code on my aspx file;
Add Error To Validationsummary
in web forms. thanks ! c# asp.net validation webforms share|improve this question asked May 6 '11 at 12:58 tugberk 29k31173264 By "before return", do you mean on the client before the form is submitted? –IrishChieftain May 6 '11 at 13:22 1 found the answer here : stackoverflow.com/questions/777889/… nearly the exact duplicate :S well, it sucks :S –tugberk May 6 '11 at 13:29 I voted my own question to close :S still need 4 votes guys. help me out here. –tugberk May 6 '11 at 13:30 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote Whenever I find this situation this is what I do: var val = new CustomValidator() { ErrorMessage = "This is my error message.", Display = ValidatorDisplay.None, IsValid = false, ValidationGroup = vGroup }; val.ServerValidate += (object source, ServerValidateEventArgs args) => { args.IsValid = false; }; Page.Validators.Add(val); And in my ASPX code I have a ValidationSummary control with a ValidationGroup set to the same value as vGroup. Then, after I have loaded as many CustomValidators (or any other kind of validators) by codebehind as I want I simply call Page.Validate() if (Page.IsValid) { //... set your valid code here } The call to Page.Validate() calls the lambda-linked method of all code-behind inserted validators and if any returns false the page is invalid and returned with n
Posted on July 4, 2014 by briancaos This trick is especially useful when you have custom code to be executed after
Add Error Message To Validation Summary Using Jquery
your form have been submitted, and still wishes to communicate add custom validator to validation summary an error the same way as you communicate form validation errors. Imagine the following ValidationSummary:
In general, field specific validation shows up next to a control that it is validating (assuming you put the validators next to it). But what http://justgeeks.blogspot.com/2010/11/adding-item-to-validationsummary.html about validation that happens in a Domain Service Class or your Custom http://weblogs.asp.net/ashicmahtab/putting-messages-into-a-validationsummary-control-from-code BLL for example? These exceptions will by default be caught by the application and show as a nasty error to the user, or go to the error page. This is hardly the desired behavior for a validation error. First I like to change the default behavior of bubbling up to the to validation application to be caught to being handled at the button or page level. To do this I put a try-catch in my button action or other applicable event that you can tap into. In the catch, it would be ideal to add a custom error message to the ValidationSummary. How do we do that though? Thankfully, it is quite easy to add an to validation summary item to the ValidationSummary. The key is that the Page has a Validators property that all validators are automatically added to when you put them on your .aspx page. The problem is that we don't have a CustomValidator. Thus we need to create a CustomValidator, but what a pain really since we only want to use it when we actually have an exception in our BLL. My solution is to create method to encapsulate the logic to create a new CustomValidator and add it to the Page's Validators collection. So that it can easily be accessed on any page, I have implemented it as an Extension to the Page class. Below is the code to do so. namespace MyExtensions { public static class PageExtensions { public static void AddValidationSummaryItem(this Page page, string errorMessage) { var validator = new CustomValidator(); validator.IsValid = false; validator.ErrorMessage = errorMessage; page.Validators.Add(validator); } } } To use this method just put the using MyExtensions; statement at the top of your code-behind of the page that you want to use it on. Then
to show some messages that are not the ErrorMessages of validators. An approach to do this could be to a) ScriptManager.RegisterClientScriptBlock to show a popup (YUCK!!) b) Put in a label and set its Text from code. This could be an option, only the error message will be in the label and not inside the ValidationSummary. This does not look good as some errors will be shown neatly in a ValidationSummary whereas others will eb shown in a separate label. c) This is my favourite. Here's what you do: In code behind, when you want to add a message to the ValidationSummary, just do this: RequiredFieldValidator req = new RequiredFieldValidator(); req.ValidationGroup = "vgInput"; req.ErrorMessage = "Your message goes here."; req.IsValid = false; Page.Form.Controls.Add(req); req.Visible = false; Notice the last three lines. One causes page validation to fail, the next adds the validator to the page's controls so it can have effect (validators must be inside the form tag). The last hides the validator so the error message is shown only in the ValidationSummary and not at the location of the validator (which is at the end of the form as it's added dynamically. Hope that helps. 1 Comment ha ha haaaaa he eh heh heeh ee adssd - Friday, February 13, 2009 12:30:14 PM Comments have been disabled for this content. Terms Of Use - Powered by Orchard