Geeks With Blogs
kazim mehdi

1. Set EnableScriptGlobalization=true on the script manager

<asp:ScriptManager runat="server" EnableScriptGlobalization="true" />

2. Use the  Sys.CultureInfo.CurrentCulture command either inside the alert or through Firebug console to check the CultureInfo at the client side

 

 

firebug-CurrentCulture-01
firebug-CurrentCulture-02
firebug-CurrentCulture-Details-01
firebug-CurrentCulture-Details-02

 

 

3. use Number.parseLocale instead of pareFloat so that it get correct value according to the current culture.


4. to update the value back with current culture use localeFormat

<script type="text/javascript">
function formatNumber()
{

var tbNumber = $("input[id$='tbNumber']");

var value = tbNumber.val();

var dValue = Number.parseLocale(value);
if (isNaN(dValue))
{
alert("invalid input please enter decimal values only");
}
else
{
var noOfDigitsAfterDecimal = 2;
var pattern = "N" + noOfDigitsAfterDecimal;
var finalValue = dValue.localeFormat(pattern);
tbNumber.val(finalValue);
}
}

</script>

 

Sample to play around

Content for ASPX page.

 

<table border="0" cellpadding="5" cellspacing="5">
<tr>
<td>
Enter number :
</td>
<td>
<asp:TextBox ID="tbNumber" runat="server" onchange="formatNumber();" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLoadDefaultValue" Text="Load default value" runat="server" OnClick="btnLoadDefaultValue_Click" />
</td>
<td>
<asp:Button ID="btnGetValue" Text="Process" runat="server" OnClick="btnGetValue_Click" />
</td>
</tr>
<tr>
<td>
Default value:
</td>
<td>
<asp:Label ID="lblDefaultValue" runat="server" />
</td>
</tr>
<tr>
<td>
Typed value:
</td>
<td>
<asp:Label ID="lblResult" runat="server" />
</td>
</tr>
</table>

 

 

paste the above mentioned javascript in the page

 

Content for Code behind.

protected void Page_Load(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("da-DK");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("da-DK");

if (!IsPostBack)
{
double dValue=2.898d;
lblDefaultValue.Text = dValue.ToString();
}
}

protected void btnLoadDefaultValue_Click(object sender, EventArgs e)
{
tbNumber.Text = lblDefaultValue.Text;
}

protected void btnGetValue_Click(object sender, EventArgs e)
{
lblResult.Text = tbNumber.Text;
}
Posted on Thursday, May 12, 2011 11:34 AM asp.net , ajax , Javascript | Back to top


Comments on this post: ASP.NET AJAX:Localization Client Side

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © kazim sardar mehdi | Powered by: GeeksWithBlogs.net