简体   繁体   English

具有html属性的Sitecore项-Webforms

[英]Sitecore Item with html attributes - Webforms

I have this code origin in a layout: 我在布局中有以下代码来源:

<div class="pm-single-post-img-container" style="background-image:url(images/01.jpg);">
    <div class="pm-single-post-title full-width">
        <p>
        <sc:Text runat="server" Field="Title" />
        </p>
    </div>
</div>  

Also i have a image field , and want to get image url set to div background , like this : 我也有一个图像字段,并且想要将图像URL设置为div背景,像这样:

<%
Sitecore.Data.Fields.ImageField imageField = Sitecore.Context.Item.Fields["Image"];
var thumbnailUrl = string.Empty;
if (imageField != null && imageField.MediaItem!=null)
{
    thumbnailUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem);
}
%>
<div class="pm-single-post-img-container" style="background-image:url(<%=thumbnailUrl%>);">
    <div class="pm-single-post-title full-width">
        <p>
            <sc:Text runat="server" Field="Title" />
        </p>
    </div>
</div>

But this way has error when the page in edit mode , because use block code. 但是这种方式在页面处于编辑模式时会出错,因为使用了阻止代码。 If you have another solution for this case , please share for me . 如果您对此案还有其他解决方案,请与我分享。 Thanks ! 谢谢 !

Try to avoid to use code blocks. 尽量避免使用代码块。

Your page(aspx.cs) or control(ascx.cs) can have public property. 您的页面(aspx.cs)或控件(ascx.cs)可以具有公共属性。

public string BackgroundUrl
{
    get
    {
        Sitecore.Data.Fields.ImageField imageField = Sitecore.Context.Item.Fields["Image"];
        var thumbnailUrl = string.Empty;
        if (imageField != null && imageField.MediaItem != null)
        {
            thumbnailUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem);
        }
        return thumbnailUrl;
    }
}

And you can use it in your code 您可以在代码中使用它

<div class="pm-single-post-img-container" style="background-image:url(<%=BackgroundUrl%>);">
    <div class="pm-single-post-title full-width">
        <p>
            <sc:Text runat="server" Field="Title" />
        </p>
    </div>
</div>

If you get this error "The Controls collection cannot be modified because the control contains code blocks" follow Presentation Component Troubleshooting 如果您收到此错误“由于控件包含代码块,则无法修改控件集合”,请遵循Presentation Component故障排除

Added about Page Editor image editing after question in comments: 在评论中的问题后添加了有关页面编辑器图像编辑的内容:

There is no way to do editing of background image of div in Sitecore out of the box. 开箱即用,无法在Sitecore中编辑div的背景图片。 But you can render tag instead of background image for PE mode. 但是对于PE模式,您可以渲染标签而不是背景图像。 For example: 例如:

<sc:Image Field="Image" ID="ImagePE" runat="server" Visible="false">
<sc:Text runat="server" Field="Title" ID="TitlePE" Visible="false"/>
<div runat="server" ID="DivNormalMode">
 ... your current code
</div>

And control visibility by code behind 并通过后面的代码控制可见性

if (Sitecore.Context.PageMode.IsPageEditor)
{
    ImagePE.Visible = true;
    TitlePE.Visible = true;
    DivNormalMode.Visible = false;
}

While changing markup and styles you can achieve pretty good appearance in PE mode and ability to edit image. 在更改标记和样式时,您可以在PE模式下获得相当不错的外观并可以编辑图像。

Or, if you have WYSIWYG requirements you should have Sitecore Edit Frame for your image field . 或者,如果您有“所见即所得”的要求,则应在“图像字段”中使用Sitecore编辑框架 You will be able to edit background image as field. 您将能够将背景图像编辑为字段。

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

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