.Net coding can be frustrating, when you're trying and failing to do something simple. In this case, I was trying to enable or disable a RequiredFieldValidator based on some criteria. I knew the ID of the controls I was trying to enable, but due to various templates and custom controls, a normal Page.FindControl() call didn't work - ASP.Net prepends a load of extra guff onto your original ID.
A hack that I've see used in cases like this is to hardcode the newly-nested ID, eg:
Obviously this is not ideal, as if the page template changes in any way, our code will stop working.
The quick fix needed in my particular case was to use a better version of FindControl - one which recursed through every control on the page if need be.
Luckily, thanks to the wonders of Google and various useful bloggers, I was soon spoiled for choice for suitable functions.
The solution I chose to use was from:
...and it was further refined here:
As an aside, I still don't understand why Microsoft make some things so needlessly complex. I much prefer the PHP way of doing things when it comes to form fields - they are all just readily accessible in an associative array.
- Airline Entertainment company iPad app
- London Web Summit 2012
- Highly charged viral Facebook campaign
- Celerity now runs on solar power
- Joomla site management
- Large corporate site build with content population
- Our office now has a wood stove!
- Templating for Mailshots
- Energy usage at Celerity
- Templating work for Contact Lens site