Saturday, May 28, 2011

How to Add validations controls Programmatically

I was looking for some server side validation message which can done without posting back to server. I tried doing javascript and writing that script to literal control and I have been thinking of doing a better way. But here is the cool way to do this.

// Dynamically adding Validation control
RequiredFieldValidator Validator = new RequiredFieldValidator();
Validator.ErrorMessage = "No data to download for your request.";
// Validation group must be specified as to which group you need to bind
Validator.ValidationGroup = "Group1";
Validator.IsValid = false;
Validator.Visible = false;
Page.Form.Controls.Add(Validator);

All you need to do is Depending on your need you need to change message and validation group. We need to set up validation group properly.


Happy coding Smile

No comments:

Post a Comment