0

I'm trying to get CurrentUserLocale, and I get it server-side from my CodeBehind.

This is what I do :

    protected void Page_Load(object sender, EventArgs e)
    {
        lang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
    [...]
    }

lang is a global stringvariable.

I have this function :

    public string getLang()
    {
        return lang;
    }

And in my JS code :

<script type="text/javascript">
    $(function () {
        var language = <%# getLang() %>;
        $("#datetimepickerdebut").datetimepicker({
            locale: language, // Eventuellement à modifier pour le multilingue
            showTodayButton: true,//Bouton d'accès rapide à la date-heure actuelle
            defaultDate: moment({h:0, m:0})/*Heure par defaut : le jour même à 00:00*/
        });
        $('#datetimepickerfin').datetimepicker({
            locale: language,
            showTodayButton: true,
            defaultDate: moment({h:23, m:59})
        });
    });
</script>

I've tried with <% GetLang(); %>, with <%= GetLang(); %>, with or without semi-colon. The calendar works well with locale:'fr' and when I set var language = 'fr'

Do you have any idea ?

Yann

1
  • 2
    Why don't you have a hidden field on the page that is populated with the lang value during the OnPageLoad event and just read the value from that field in the javascript code? Commented Aug 28, 2015 at 13:50

1 Answer 1

3
$language = '<%= getLang() %>';

Notice the single quotes around it? You have to keep in mind that the value needs to be a string in the client side, so you need to put it within quotes.

The <%# %> syntax is for data binding, you would have to call bind on a parent object for that to work.


Alternatively, as Stoward94 suggests, you can use a hidden field.

//markup
<asp:HiddenField ID="LanguageHF" runat="server" ClientIdMode="static" />

//js    
$language = document.getElementById('LanguageHF').value;

//code behind   
LanguageHF.Value = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
Sign up to request clarification or add additional context in comments.

1 Comment

Had to wait 7 minutes ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.