Friday, September 18, 2009

Be careful using hard coded ValidationGroup in a User Control in ASP.NET

Validation Groups are great in ASP.NET. They allow you to validate a group of controls. You tell ASP.NET what Validation group they are in by specifying a ValidationGroup property on each of the controls that you want in that group.

Simple enough. However, I found out the hard way that if you use the ValidationGroup in a user control you have just made it difficult if not impossible to use your user control more than once on a page.

After I realized that I had the issue, and thought about it for a minute it made sense. Let’s say I have two instances of my user control on my page. There are now twice as many controls in the group than intended. This means unless all the fields in both the user controls pass the validation you will have issues such as submit buttons not submitting (assuming they cause validation to fire).

You can verify this, by looking at the html source for your page. In the JavaScript code, search for “ValidationGroup”. You will see that the validation group is just as you typed it your user control. This means that unlike most other things in ASP.NET, the ValidationGroup is NOT automatically adjusted to take its location in the hierarchy of controls.

What does that mean? That means you have to do this yourself. :) All you have to do is set the ValidationGroup dynamically. The easiest way to do this is to create a property in your code behind.

protected string UniqueValidationGroup { get { return ClientID + "MyValidationGroup"; } }

I chose to use take advantage of the ClientID always having the full path from the page to user control as the ClientID. This means that it is unique from JavaScript. I concatenate that with my ValidationGroup name to give a 100% unique ValidationGroup name. That way, no matter where or how many times your user control is used, the ValidationGroup will be unique.

Now that we have have a unique ValidationGroup name, we need to set it dynamically instead of hardcoding it. You can do this in the code-behind, but I think that can be hard to maintain. I like doing it in the code-behind. Where ever you have ValidationGroup hardcoded, just change it to something like the following (depending on what you called your property in your code-behind).

ValidationGroup='<%# UniqueValidationGroup %>'

Check the JavaScript that is generated from this. You will see that the ValidationGroup is set to a long unique name.

6 comments:

Anonymous said...

Thank you.

Anonymous said...

Worked for me too, thanks.

Anonymous said...

This did not work for me.

I can't use <%#AccessorName %> to Set values in an asp object.

Anonymous said...

Worked for me too[2]

Thank you for this post

Paul Russo said...

Thank you - I was thinking on the lines of a viewstate property and page init override - but this is so much slicker!

aparna john said...

Hi,If you're building an entire web site to sell your product or Web Design Cochin service then you may want to have a professional build you a couple of web page design templates than you can modify easily.Thanks.......