您可以在ASP.NET中使用全局RESX文件作为CodeBehind并绑定到Web表单标签吗?

发布于 2025-01-31 18:43:46 字数 951 浏览 2 评论 0原文

我有兴趣在“ App_globalresources”文件夹中仅使用全局资源文件,而在Web表单标签中没有单独的文件中的“ App_localResources”中的单独文件。

我喜欢将所有资源字符串集中到一个地方的想法。

我的app_globalresources中的我的resx文件具有以下属性:

Build Action: Embedded Resource
Copy to Output Directory: Do not copy
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources

变量时可以很好地

在调用标记中的

工作在背后的代码中正常工作:

dim strtest as string = myStrings.this_is_a_a_test_string

问题会发生。

当我尝试将全局资源字符串绑定到Web表单控件时, id =“ lblfullname” runat =“ server” text =“<%$资源:mystrings,this_is_i_a_test_string%>” AssocedControlid =“ txtfullname”></asp:label>

Web服务器显示此错误:

带有键'this_is_is_a_test_stest_string'的资源对象。

我确实注意到了,如果我更改了,我确实注意到了RESX将正确绑定到标签控件的“构建动作”。

但是,问题是字符串将不再在标记或CodeBehind中使用。

因此,是否有一种方法可以将一个全局资源文件用于标记,CodeBehind和Web表单标签控件?

或者,我是否被迫使用两个独立的资源文件?

在这一点上,我承认我完全感到困惑。

I am interested in using only Global resource files in the "App_GlobalResources" folder and not having separate files in the "App_LocalResources" for web form labels.

I like the idea of centralizing all the resource strings in one place.

My resx files in App_GlobalResources have the following properties:

Build Action: Embedded Resource
Copy to Output Directory: Do not copy
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources

This works fine when calling a variable in markup such as:

<p><%=MyStrings.This_is_a_test_string%></p>

It also works fine in the code behind:

Dim strTest As String = MyStrings.This_is_a_test_string

The problem occurs when I attempt to bind a global resource string to a web form control such as:

<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, This_is_a_test_string %>" AssociatedControlID="txtFullName"></asp:Label>

The web server displays this error:

The resource object with key 'This_is_a_test_string' was not found.

I did notice if I change "Build Action" to "Content" that the resx will correctly bind to the Label control.

However, the problem is the strings will no longer work in the markup or the codebehind any longer.

So, is there a way to use ONE global resource file for the markup, codebehind, and for web form Label controls?

Or, am I forced to use two separate resource files?

At this point I'll admit that I'm thoroughly confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2025-02-07 18:43:46

我无法弄清楚如何使用我都可以在诸如:

dim strtest as String = myStrings.testString

的代码中明确使用的全局.RESX文件,还可以绑定到WebForm控制,例如:

&lt; asp:label ID =“ lblfullname” runat =“ server” text =“&lt;%$ resources:mystrings,testString%&gt;” AssocedControlId =“ TXTFULLNAME”&gt;&lt;/asp:label&gt;

因此,我认为最简单的解决方案是使用我拥有的全局resx文件,而不是将它们绑定到Web表单控件,我简单地设置了Web表单控件像这样明确使用其ID:

lblfullname.text = myStrings.fullname

我所做的是使用{}将占位符放在实际的控制文本中,以便我可以查看是否忘记了设置任何一个诸如:

&lt; asp:label ID =“ lblfullname” runat =“ server” text =“ {这是一个test String}” AssoceControlid =“ txtfullname”&gt;&lt;/asp:label&gt;

I wasn't able to figure out how to use a global .resx file that I could both use explicitly in the code behind such as:

Dim strTest As String = MyStrings.TestString

and also bind to a webform control such as:

<asp:Label ID="lblFullName" runat="server" Text="<%$ Resources:MyStrings, TestString %>" AssociatedControlID="txtFullName"></asp:Label>

Therefore, I decided the simplest solution was to use the global resx files I had and instead of binding them to web form controls I simply set the web form controls explicitly using their ID in the codebehind like so:

lblFullName.Text = MyStrings.FullName

What I did was put a placeholder in the actual control text using { } so I could see if I forgot to set any of the text such as:

<asp:Label ID="lblFullName" runat="server" Text="{THIS IS A TEST STRING}" AssociatedControlID="txtFullName"></asp:Label>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文