More in this section

Forums / Developing with Sitefinity / Set Image Control to Item From Image Library

Set Image Control to Item From Image Library

3 posts, 0 answered
  1. Kevin Lonigro
    Kevin Lonigro avatar
    7 posts
    Registered:
    28 Apr 2006
    31 Aug 2008
    Link to this post
    Would like to dynamically / programatically set the source of an image control to an item in an Image Library.  Please provide an outline of the relevant helper library classes and steps required for this task.

    Thank you.
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    01 Sep 2008
    Link to this post
    Hello Kevin,

    You can find useful information for the Images & Documents API in the following topics of the Developer Manual:
    1. Images and Documents API Overview
    2. Important Interfaces of Libraries Module
    3. Libraries Overview
    To achieve the required functionality you can wrap the ASP.NET Image control in a user control and use the API to get the url of the library item. Here is a simple example:

    .ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ImageWrapper.ascx.cs" Inherits="ImageWrapper" %> 
     
    <asp:Image ID="Image1" runat="server" /> 

    .ascx.cs
    using System; 
    using System.Collections; 
    using System.Configuration; 
    using System.Data; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using Telerik.Libraries; 
    using System.ComponentModel; 
    using Telerik.Cms.Engine; 
     
    public partial class ImageWrapper : System.Web.UI.UserControl 
        #region Properties 
     
        [Category("Behavior")] 
        public Image ImageControl 
        { 
            get 
            { 
                return this.Image1; 
            } 
            set 
            { 
                this.Image1 = value; 
            } 
        } 
     
        [Category("Data")] 
        public string ProviderName 
        { 
            get 
            { 
                if (string.IsNullOrEmpty(this.providerName)) 
                    return LibraryManager.DefaultContentProvider; 
                else 
                    return this.providerName; 
            } 
            set 
            { 
                this.providerName = value; 
            } 
        } 
     
        private LibraryManager Manager 
        { 
            get 
            { 
     
                if (this.manager == null || this.manager.Provider.Name != this.ProviderName) 
                    this.manager = new LibraryManager(this.ProviderName); 
                return this.manager; 
            } 
        } 
        #endregion 
     
        protected void Page_Load(object sender, EventArgs e) 
        { 
            // Get a library with name "Party Pictures" 
            ILibrary library = this.Manager.GetLibrary("Party Pictures"); 
     
            if(library != null
            { 
                // Get a list with all items in the selected library 
                IList items = this.Manager.GetContent(0, 0, "Publication_Date DESC", library.ID); 
     
                if (items.Count > 0) 
                { 
                    // Get the first item in the list 
                    IContent cnt = items[0] as IContent; 
                    Image1.ImageUrl = this.ResolveUrl(String.Concat(cnt.Url, this.Manager.Provider.ContentExtension)); 
                    Image1.AlternateText = cnt.GetMetaData("AlternateText"as string
                } 
            } 
             
        } 
     
        private LibraryManager manager; 
        private string providerName; 

    For your convenience I have attached the full example. To test the control, you need just to upload it through Sitefinity interface. Also, you should create a library with name "Party Pictures" and upload at least one image in it.

    Please, describe in details if there is some misunderstanding.

    Kind regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Kevin Lonigro
    Kevin Lonigro avatar
    7 posts
    Registered:
    28 Apr 2006
    01 Sep 2008
    Link to this post
    Thanks for the suggestions -- I had forgotten about IContent having Url property.
Register for webinar
3 posts, 0 answered