How to change double format of current culture asp.net

978 views Asked by At

I have a web page with the next TextBox:

<asp:TextBox ID="txtSum" runat="server"></asp:TextBox>  

and compareValidator to validate it:

<asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic" ControlToValidate="txtSum" ErrorMessage="less than 0" ValueToCompare="0" Type="Double" Operator="GreaterThan">  
</asp:CompareValidator>

when I set Culture of the page to Russian, the compare validation does not work well.
I found the reason is that format number is different between English and Russian.
I tried to change the format as follow:

NumberFormatInfo format = CultureInfo.CreateSpecificCulture("en-US").NumberFormat;  
Thread.CurrentThread.CurrentCulture.NumberFormat = format;

but it does not work. in debug I see the NumberFormat of culture has changed but in the page I get the message less than 0.

how can I solve it?

1

There are 1 answers

3
Amit On

Issue you are facing is due to browser culture. Browser culture is Russian and your code execution on IIS might have culture english. Always browser culture is getting passed. You need to parse double value using invariant culture or change current UI culture so that it will work.

protected override void InitializeCulture()
{
    Page.Culture = CultureInfo.CreateSpecificCulture("en-US");
    Page.UICulture = CultureInfo.CreateSpecificCulture("en-US");    
}