R – Set the Culture for an asp.net RangeValidator

asp.netcultureinfocurrentculturerangevalidator

An asp:RangeValidator takes it's culture info from the page/ thread and all advice online around getting the right culture seems to be to set the CultureInfo on the thread.

Rick Strahl's west wind blog is typical advice

However, due to problems running the server code in this way, we are stripping out the code we were using previously that followed this advice.

My question is: is there a way for me to set the culture of a RangeValidator without messing with the main page/ thread culture. I want the RangeValidators to respect the browser settings, but unfortunately they are respecting the server culture instead.

Best Answer

As there have been no answers to this I am going to answer my own question.

The answer is that this does not appear to be possible- there is no way to set the culture for a validator directly.

The way in which I was able to accomplish my goals of having the server-side code running always in an english culture but then causing the validators to work in the browser culture was to set the thread currentculture at the end of Page_PreRender. Therefore up to this point the code runs in english culture, but we set the culture in time for it to be in effect for when the asp.net runtime uses it to set up the validators.

This works fine for the cient-side action of the validators, in my case allowing users on a french browser to enter numbers in a french format. But there is a further gotcha- if you have code to validate server side on submit/ postback, this validation will now fail- it appears that on postback the original culture settings of the validator are not retained, they use the culture in place at the time they are validated server-side: of course the french numbers are not then viewed as valid, and the validation fails.

I hope this helps anyone else who encounters the same issue