views:

369

answers:

1

Ok, I have a major issue with a control I am using.

The site I'm doing uses custom units for some fields in their checkout (who decided that horses should be measures in hands for god's sake?!), and we have to be able to validate based on the unit selected, i.e. they can have between 15 and 40 hands (or something) which is 200 - 900 cm (these numbers are completely random fyi).

normally, this would be validated using server-side validation to allow us to process the current unit, convert the input appropriately and compare to the dynamic limits.

Unfortunately, these controls are being dynamically rendered onto the page on page load, and so the server side validation isn't firing as they don't technically exist on post back.

What I intend to do to avoid this is to use another method that we've used before, similar to a standard regex validator but hacked to allow code behind (I didn't write it but they seem to work).

This is not on the page, however, and so I would need to access the page object via HttpContext or some other method in order to get the correct units from a related dropdown, can anyone suggest either a way to do this or a better way to validate the controls?

Oh, and yes this is incredibly convoluted and random.

Cheers, Ed

+3  A: 

There are a few points to make here: Firstly, you can get postbacks off controls that are dynamically generated, so long as you re-generate them with a consistent ID at the start of the each postback event, once you've recreated them ASP.NET will re-connect the ViewState and whatnot and then trigger your callback method after that.

Next point, you can use the ASP.NET ajax framework to call a static method on your page class using some javascript (essentially it becomes a mini-web service). Checkout this link on how to do it. That might enable you to write some javascript to call a validate method on the server side. It's static only though so you have to pass in all the data you need from your client side code.

Finally, you could have multiple regex validators, one for each unit, then use some javascript in the onchanged event for the units field to enable/disable the right one. I've not done it myself, but I believe you can easily turn off a validator via javascript. (Checkout this link)

d4nt
Good thinking on the multiple validators, I'm going to take that route. I'll accept the answer when SO stops being buggy and let's me click the tick =D
Ed Woodcock