简体   繁体   中英

Setting media URLs in Sitecore

There are a few similar questions here and here but I have not been able to resolve my problem.

I want to have a 'Download Plans' link that will download an image in Sitecore 6.4.1. I have successfully set images as backgrounds and such, but linking as an href has proven difficult.

In my ASCX control which is set as a sublayout, the front-end code looms like this:

<a class="btn btn-default" id="download_plans" runat="server" ClientIDMode="Static">
    Download Plans
</a>

And my code-behind has this:

private void Page_Load(object sender, EventArgs e)
{
    if (((Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["Floor Plan Image"]).MediaItem != null)
    {
        var url = Sitecore.StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(((Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["Floor Plan Image"]).MediaItem));
        download_plans.HRef = url;
    }
}

In another of my controls I have the same code, except instead of adding the HRef attribute, I added the value as a style using the code below. This works fine

download_plans.Attributes.Add("style", string.Format("background-position:right bottom; background-image:url(\"{0}\"); background-repeat:no-repeat; background-size:contain", url));

In Sitecore, the field is called "Floor Plan Image" - is this the correct string to use when retrieving the element (It is "Image" in my other control, which is very generic)? Currently nothing is being added to the element at all.

Any suggestions?

You say you have the same snippet of code in another one of your controls. If it's not working there, the only real reason I can think of is; that your other control is hosted on a page of a different template type - one that does not include Floor Plan Image.

Check that your control is hosted on the Page Template you expect.

Ideally, you should try not relying on Sitecore.Context.Item in your components; for this reason and many more.

Please check on element class "btn btn-default" is any background property is not applied on this via CSS if it has some property then first remove this and try.

Another way you can try by removing class attribute if it's work then some problem in css.

Let me know if it's not work i'll give you some useful ideas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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