0
votes

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?

2
Have you tried to debug the code and check if the condition is met? And the code for setting HRef is executed at all? "Floor Plan Image" is a correct string for the field name. Just remember to use Name (not Display Name). - Marek Musielak
Good idea- I attached CS to my IIS process, and the condition is met. The HRef field is being set on the download_plans element, but it's still not showing in the browser. - Irish Yobbo
Try changing the control to <asp:HyperLink CssClass="btn btn-default" ID="download_plans" runat="server" ClientIDMode="Static"> and set the NavigateUrl instead - jammykam

2 Answers

0
votes

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.

0
votes

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.