简体   繁体   English

具有格式化文本字段的标签/文字控件

[英]Label/Literal control with formatted text field

I have discovered that it is possible to populate resource strings with variable information using string.format, see below: 我发现可以使用string.format使用变量信息填充资源字符串,如下所示:

String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales)

I can display this on my page using: 我可以使用以下方法在页面上显示此内容:

<p><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></p>

In some cases I have a Label or Literal (or any control) which might dynamically hide or show content. 在某些情况下,我有一个Label或Literal(或任何控件),它们可以动态隐藏或显示内容。 Ordinarily I would populate those using: 通常,我会使用以下方法填充这些内容:

<asp:Literal ID="Literal1" Text="<%$ Resources:Temp,ContactUs %>" runat="server" />

I now would like the same String.Format functionality whilst still using controls. 我现在想在仍使用控件的同时使用相同的String.Format功能。 I found Display value of Resource without Label or Literal control but this doesn't actually work for me, it just writes out '<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>' on the page (not the content, that actual string). 我发现没有标签或文字控件的“资源”的显示值,但这实际上对我不起作用,它只是在页面上写出“ <%= GetGlobalResourceObject(“ Messages”,“ ThankYouLabel”)%>“(而不是内容,该实际字符串)。

UPDATE: 更新:

I have found a solution which works with some controls: 我找到了可以与某些控件一起使用的解决方案:

<asp:Label runat="server" ID="temp"><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></asp:Label>

However, this works doesn't work for Literal controls as they don't allow child controls. 但是,此方法不适用于文字控件,因为它们不允许子控件。 I'd prefer to keep using Literal's as they are the cleanest in terms of code generated, so still seeking a solution. 我宁愿继续使用Literal,因为就生成的代码而言,它们是最干净的,因此仍在寻找解决方案。

asp:Literal doesn't support <%= %> construct, and doesn't allow child controls (I mean something like <asp:Literal runat="server"><%= ... %></asp:Literal> ). asp:Literal不支持<%= %>构造,并且不允许子控件(我的意思是<asp:Literal runat="server"><%= ... %></asp:Literal> )。

But if you use data binding, your could use data-binding expresions <%# ... %> : 但是,如果使用数据绑定,则可以使用数据绑定表达式 <%# ... %>

<asp:Label runat="server" Text="<%# string.Format(...) %>"></asp:Label>

To make this work you should ensure that either implicit or explicit data binding for your controls is used. 为了使这项工作有效,您应确保为控件使用隐式或显式数据绑定。 Otherwise the control like this without binding outputs nothing . 否则,这种没有绑定的控件将不会输出任何内容

This workaround is a little bit complex. 此解决方法有点复杂。 Consider using either asp:Label control, or set the Text property from the code behind. 考虑使用asp:Label控件,或从后面的代码中设置Text属性。

To solve my problem I have actually had a second look at how I am displaying content and found that a lot of times the Literals and Labels could be dropped in place of plain HTML code. 为了解决我的问题,我实际上对我如何显示内容进行了第二次查看,发现很多时候可以删除文字和标签来代替纯HTML代码。 I can then use my preferred method <%= ... %> to display content. 然后,我可以使用首选方法<%= ... %>来显示内容。

You could use an asp:PlaceHolder control instead of a Literal . 您可以使用asp:PlaceHolder控件代替Literal
PlaceHolders can contain child controls; PlaceHolders 可以包含子控件; they also support <%= … %> -style " displaying expressions ". 它们还支持<%= … %>样式的“ 显示表达式 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM