+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / AssetsField Control tutorial

AssetsField Control tutorial

3 posts, 0 answered
  1. Chad
    Chad avatar
    9 posts
    Registered:
    23 Aug 2012
    13 Sep 2012
    Link to this post
    I have a module built using the Module Builder and Im editing the widget template to display a link to a uploaded document but I want the link to say "Download Document" instead of the filename of the document that was uploaded. How can I do this using the AssetsField? or is there another control that I can use to do this for me. I tried the Hyperlink control and that does not work. Im at a loss.

    Also, it would be helpful if Sitefinity had a API controls so I could figure it out without seeking help on the forums. I have searched the whole site and can't find any documentation on this control.
  2. Lori
    Lori avatar
    4 posts
    Registered:
    25 Feb 2009
    29 Aug 2013
    Link to this post
    Does anyone have an answer to this one? I am having the same problem.
  3. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    03 Sep 2013
    Link to this post
    Hello,

    What you need to do is create a class library called MyClassLibrary for example. Inside it create a class file called CustomAssetsField.cs, inherit from AssetsField and replace the template of the control with a custom one. When you're using the document mode of the AssetsField control, the link to your document is displayed in the template of the AssetsField by a DocumentLink control (which is a Sitefinity control):

    <sf:DocumentLink id="documentLink" runat="server" />

    That control however does not expose a text property anymore so that is why we have to change the template of the AssetsField control itself.

    In the CustomAssetsField.cs file of the class library you have created use the following code:

    using System;
    using System.Linq;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    using Telerik.Sitefinity.Web.UI.Fields;
    using Telerik.Sitefinity.Web.UI.Fields.Enums;
       
    namespace MyClassLibrary
    {
        public partial class CustomAssetsField : AssetsField
        {
            #region Ctor
       
            public CustomAssetsField()
            {
                this.LayoutTemplatePath = CustomAssetsField.layoutTemplatePath;
            }
       
            #endregion
       
            #region Control references
       
            /// <summary>
            /// Gets the document link control.
            /// </summary>
            /// <value>The document link control.</value>
            protected virtual HyperLink DocumentLinkControl
            {
                get
                {
                    return this.Container.GetControl<HyperLink>("documentLink", true);
                }
            }
       
            #endregion
       
            #region Public and overriden methods
       
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
       
                if (this.DisplayMode == FieldDisplayMode.Read && this.WorkMode == AssetsWorkMode.SingleDocument)
                {
                    if (this.ContentLinks != null && this.ContentLinks.Length > 0)
                    {
                        //get the document
                        var contentLink = this.ContentLinks.FirstOrDefault();
                        var libraryManager = LibrariesManager.GetManager(contentLink.ChildItemProviderName);
                        var document = libraryManager.GetDocument(contentLink.ChildItemId);
                        if (document != null)
                        {
                            //get the document url
                            var docUrl = document.ResolveMediaUrl();
                            this.DocumentLinkControl.NavigateUrl = docUrl;
       
                            //get the dynamic item
                            var dynamicModuleManager = DynamicModuleManager.GetManager(contentLink.ParentItemProviderName);
                            var type = TypeResolutionService.ResolveType(contentLink.ParentItemType);
                            var dynamicItem = dynamicModuleManager.GetDataItem(type, contentLink.ParentItemId);
       
                            //set the link text
                            this.DocumentLinkControl.Text = "This is the text that is going to be displayed"
                        }
                    }
                }
            }
       
            protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
            {
                //keep this method to prevent the default behavior
            }
       
            #endregion
       
            #region Private fields and constants
       
            //the path to your ascx file
            private static readonly string layoutTemplatePath = "~/CustomAssetsField.ascx";
       
            #endregion
        }
    }

    In your class library create an ascx file and name it CustomAssetsField.ascx and paste the following code in it:

    <%@ Control Language="C#" %>
    <%@ Register TagPrefix="sitefinity" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI" %>
    <sitefinity:SitefinityHyperLink ID="documentLink" runat="server" target="_blank" />

    Build the solution and open the project's backend. Navigate to the page where the widget of your dynamic module is dropped and go to edit its template through the widget's designer (clicking on the Edit button of the widget).

    Now you will need to replace the default AssetsField control with the one you have just created. To do so you first need to register the control like so (taking in mind the above example's namespace and class name):

    <%@ Register Assembly="MyClassLibrary" Namespace="MyClassLibrary" TagPrefix="uc" %>

    You will then be able to use the control on the template like so for example:

    <uc:CustomAssetsField ID="customAssetsField" runat="server" DataFieldName="Document" />


    Regards,
    Stefani Tacheva
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
3 posts, 0 answered