来自.resx文件的JavaScript GetLocalResourceObject

时间:2016-06-27 10:39:01

标签: javascript

我试图在javascript中从.rsex文件中获取本地资源密钥,但它没有工作,我收到错误"不可用"。

感谢您的帮助。



 var key = "errMesgLength"
                var val = eval('<%= GetLocalResourceObject("' + key + '") %>');
                lblMessage.innerHTML = val;
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

谢谢Michael的帮助。

我找到了解决方案。

在C#代码中我使用了Web方法

 [WebMethod()]
public static string GetLocalRessources(string key)
{
    var currentLanguage = (Language)HttpContext.Current.Session["CurrentLanguage"];

    if (currentLanguage == Language.French)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
    }
    else
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    }
    return HttpContext.GetLocalResourceObject("~/SelfRegistration.aspx", key, Thread.CurrentThread.CurrentCulture).ToString();
}

在JS代码中,我使用了PageMethos调用

function IsIMEI() {
    var imei = document.querySelector(".txt-imei");
    var lblMessage = document.querySelector(".lblMsgError");
    var key;
    if (imei) {
        if (imei.value !== "IMEI / Serial Number") {
            if (imei.value.toString().length > 7) {
                imei.style.border = "";
                lblMessage.innerHTML = "";
                return  true;
            }
            else {
                 key = "errMesgLength"
                PageMethods.GetLocalRessources(key, onSucess, onError);

                return false;               
            }
        }
        else {

            imei.style.border = "1px solid red";
             key = "errMesgIMEI"
            PageMethods.GetLocalRessources(key, onSucess, onError);
            return false;
        }
    }
}

function onSucess(result) {
    var lblMessage = document.querySelector(".lblMsgError");
    lblMessage.innerHTML = result;
}

function onError(error) {
        alert(error._message);
}