编程语言
首页 > 编程语言> > c#-Sitecore子版面配置中的字符串缺少正斜杠

c#-Sitecore子版面配置中的字符串缺少正斜杠

作者:互联网

我在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"" />";

结果相同.

我在这里想念什么吗?我怎样才能解决这个问题?

解决方法:

由于您希望通过两种方式来呈现信息,因此,我考虑将控件和HTML移至标记文件(ASCX),然后将这些段包装在asp:placeholder控件中.

<asp:placeholder id="imageTitle" runat="server" Visible="false">
    <sc:Image CssClass="product-image ng-scope" Field="Logo" runat="server" />
</asp:placeholder>
<asp:placeholder id="textTitle" runat="server>
    <div class="product-name ng-binding ng-scopen" ng-if="!currentProduct.imageNameHome">
       <asp:Literal id="productTitleLiteral" runat="server" />
    </div>;
</asp:placeholder>

然后,您可以在页面加载期间将代码中占位符的可见性切换到后面.

public void Page_Load{
   Item productItem = itemHelper.GetItemByPath(currentItemPath);
   Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)productItem.Fields["Logo"]);
    if (imgField.Value != "")
    {
        this.imageTitle.Visible = true;
        this.textTitle.Visible = false;
    }
    else {
        this.imageTitle.Visible = false;
        this.textTitle.Visible = true;
        this.productTitleLiteral.Text = productItem["Produkt Titel"];
    }
}

这将允许您确保业务逻辑相对于表示标记的正确封装,并且可以与.NET生命周期更好地协作.

标签:sitecore,html,c
来源: https://codeday.me/bug/20191120/2043799.html