我在 Sitecore 子布局后面的 C# 代码中有一个函数,它返回一个如下所示的字符串:
public string getProductTitle()
{
Item productItem = itemHelper.GetItemByPath(currentItemPath);
Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)productItem.Fields["Logo"]);
if (imgField.Value != "")
{
return "<sc:Image CssClass=\"product-image ng-scope\" Field=\"Logo\" runat=\"server\" />";
}
string productTitle = "";
productTitle = productItem["Produkt Titel"];
return "<div class=\"product-name ng-binding ng-scopen\" ng-if=\"!currentProduct.imageNameHome\">" + productTitle + "</div>";
}
在 ascx 中我称这个函数为:
<%= getProductTitle() %>
问题是,这就是我在运行时在 HTML 中得到的结果:
"<sc:Image CssClass=\"product-image ng-scope\" Field=\"Logo\" runat=\"server\" >";
末尾的/丢失,导致整行中断,没有显示图像。
我也试过这个:
string a = WebUtility.HtmlEncode("<sc:Image CssClass=\"product-image ng-scopen\" Field=\"Logo\" runat=\"server\" />");
return WebUtility.HtmlDecode(a);
还有这个:
return @"<sc:Image CssClass=""product-image ng-scopen"" Field=""Logo"" runat=""server"" />";
同样的结果。
我是不是漏掉了什么?我该如何解决这个问题?
最佳答案
由于 ASP.NET 页面生命周期的缘故,我看不到这个工作。
Sitecore 控件就像普通的用户控件一样,它们在服务器端运行 - 将它们作为字符串返回将在页面生命周期中为时已晚而无法呈现。
我会放置 <sc:Image />
在 ascx 页面中控制或使用 FieldRenderer
对象从 Sitecore 获取 HTML
Sitecore.Web.UI.WebControls.FieldRenderer.Render(myItem, "MyFieldName", "disable-web-editing=true");
然后您可以使用逻辑根据您的要求显示或隐藏图像字段和标题控件。这假设您使用的是 Sitecore 上下文项。
替换 <%= getProductTitle() %>
与
<sc:Image runat="server" ID="imgLogo" Field="Logo" />
<asp:Placeholder runat="server" ID="phTitle">
<div class=\"product-name ng-binding ng-scopen\" ng-if=\"!currentProduct.imageNameHome><sc:Text runat="server" ID="title" Field="Produkt Titel"/></div>
</asp:Placeholder>
然后在Page Load方法后面的代码中
var currentItem = Sitecore.Context.Item;
if(string.IsNullOrEmpty(currentItem["Logo"])
{
imgLogo.Visible=False;
}
if(string.IsNullOrEmpty(currentItem["Produkt Titel"])
{
phTitle.Visible=False;
}
更多信息在这里:
http://gettingtoknowsitecore.blogspot.co.uk/2010/01/displaying-field-values-using-server.html
关于c# - Sitecore 子布局中的字符串中缺少正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895621/