从 javascript 或代码隐藏中获取资源对象
有没有办法获取分配给 javascript 或代码隐藏中的控件的资源密钥?假设我在页面上有这个标签:
<asp:Label runat="server" ID="Label1" meta:resourcekey="Label1Resource1" />
然后在代码隐藏中添加这个:
Label1.Attributes.Add("onclick", "InspectMe(this);");
最后是Javascript:
<script language="javascript" type="text/javascript">
function InspectMe(ctl) {
// how do I get the resourcekey?
alert(ctl);
}
</script>
所以单击JS具有控制权,但是有什么方法可以读取资源键吗?请注意,我不需要实际的资源字符串(文本),我希望它返回“Label1Resource1”。
或者,代码隐藏解决方案也可以工作。我的第一反应是转向 GetLocalResourceObject,但我看不到从控件获取键名称的方法。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是简单地写下实际 ASP.Net 页面中的脚本:
您可能还会考虑使用 Globalize 库的
localize()
函数,这是一种更结构化的方法 - 您必须以某种方式编写消息数组。最后,您可以创建自己的 Handler ,它将为您编写本地化的客户端脚本。此方法也可以与 Globalize 一起使用。
选择取决于您想要实现的目标以及项目的规模。对于大型项目,Localization Handler 和 Globalize 库的组合可能是最好的选择。
The easiest way is to simply write down the script from actual ASP.Net page:
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.