Get resource object from javascript or code-behind
Is there a way to get the resource key that's assigned to a control in javascript or in code-behind? Say I have this label on the page:
<asp:Label runat="server" ID="Label1" meta:resourcekey="Label1Resource1" />
And then in code-behind I add this:
Label1.Attributes.Add("onclick", "InspectMe(this);");
And finally the开发者_StackOverflow中文版 Javascript:
<script language="javascript" type="text/javascript">
function InspectMe(ctl) {
// how do I get the resourcekey?
alert(ctl);
}
</script>
So on click JS has the control, but is there any way to read the resource key? Note that I don't want the actual resource string (the text), I want it to return "Label1Resource1".
Alternatively, a code-behind solution would work as well. My first instinct was to turn to GetLocalResourceObject, but I can't see a way to get the key name from a control.
The easiest way is to simply write down the script from actual ASP.Net page:
<script language="javascript" type="text/javascript">
function InspectMe(ctl) {
var message = '<asp:Literal id="someUniqueId" meta:resourcekey="someKey" />';
alert(ctl);
}
</script>
You might also think of using Globalize library's localize()
function, which is more structured way of doing this – you would have to wrote messages array somehow.
Lastly, you can create your own Handler that will write out localized client-side scripts for you... This method could also be used with Globalize.
The choice depends on what you want to achieve and on the size of the project. For large projects, combination of Localization Handler and Globalize library is probably the best choice.
精彩评论