Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel 15 posts 107 karma points
    Aug 22, 2013 @ 05:07
    Daniel
    0

    Setting image from media library using code behind

    I am pretty new to Umbraco still and to give perspective, I also use sitecore which may be shifting my view of this issue. I may be missing a simple and major concept of Umbraco. Anyway, the issue I am trying to resolve is just setting the logo of all my pages that is taken from a node that i named "Site Data." I am using a media picker for the field and have a usercontrol that is placed in the header of every page. Is there an easier / better way to accomplish this than as follows? :

    The user control (attached to masterpage):
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="logo.ascx.cs" Inherits="..." %>
    <a href='...'>
        <asp:Image ID="Logo" AlternateText="Logo" runat="server" />
    </a>

    The codebehind:
            protected void Page_Load(object sender, EventArgs e)
            {
                var node = new Node( Ref.SiteInfo ); // Ref is just a class I made that holds node IDs


                var file = new Media(int.Parse(node.GetProperty("headerLogo").Value));
                Logo.ImageUrl = file.getProperty("umbracoFile").Value.ToString();
            }

    Which works but doesn't feel right,

    When using sitecore, you could just use a
    <sc:Image Field="logo" runat="server" ID="HeaderLogo" />

    and in the code behind,
    HeaderLogo.Item = node;

    and it would basically just change what the "HeaderLogo" looks at (instead of looking at the current node, it would look for the field named 'logo' in the node you assigned). I was curious if I could be misssing something in the <umbraco:Item > that would act the same, but haven't found it. Is there a way to get an umbraco item to essentially change context or what node it looks at to fill a field? I am not familiar with xslt nor razor, I really like to stick to the code behind to keep code separate from html as much as possible. This is where I found the closest answer and built on it to fit my situation.

    Any information or insight is more than welcome.

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Aug 22, 2013 @ 05:43
    Kevin Jump
    0

    Hi

    within a template you can just use Umbraco Image (http://our.umbraco.org/documentation/Reference/Templating/Masterpages/umbracoimage)

    so for example

    <umbraco:image runat="server" field="headerLogo" />
    

    will do what you need.

    not 100% sure how you would do that in code behind, but you shouldn't need to. most of the time you can just use the umbraco fields, or use umbraco Macro's to achieve what you need.

    there is more info at http://our.umbraco.org/documentation/reference/templating/Masterpages/

  • Daniel 15 posts 107 karma points
    Aug 22, 2013 @ 16:41
    Daniel
    0

    Hello,

    Thanks for the reply,

    Ahhhh I didn't know about the <umbraco:image> tag. Is there a table somewhere or a reference list of all the umbraco tags somewhere or is this all the tags http://our.umbraco.org/documentation/reference/templating/Masterpages/? I looked in the documentation with no luck, it seems like most are listed on the link but intellisense also shows <umbraco:ContentType>? ... This solves most of the issue, the other step I am still fuzzy on is connecting the field="headerLogo" to the correct node. Right now, the way I have it setup, there is just the one item, Site Data that contains that field so a content editor can easily change it to anything in the media library. Is it possible to change what node the image tag is looking for the field in?

Please Sign in or register to post replies

Write your reply to:

Draft