Anders Hattestad
Mar 15, 2010
  13707
(1 votes)

Property to make one link with text and target (LinkCollection style)

If you want a property that renders in edit only one link from the LinkCollection property like this:

image

image

As you can see I needed to copy the whole PropertyLinkCollectionControl class from reflector since it’s marked as internal. Read more about that rant about that issue here.

Here is the code

[Serializable]
[PageDefinitionTypePlugIn()]
public class PropertyOneLink : PropertyLinkCollection
{
    public LinkItem MyLink
    {
        get
        {
            if (this.Links != null && this.Links.Count > 0)
                return this.Links[0];
            return null;
        }

    }
    public override EPiServer.Core.IPropertyControl CreatePropertyControl()
    {
        return new PropertyOneLinkControl();
    }
}

public class PropertyOneLinkControl : PropertyDataControl, INamingContainer
{

    // Fields
    private Control editControl;

    // Methods
    public override void ApplyEditChanges()
    {
        try
        {
            this.PropertyLinkCollection.Links = ((IPropertyLinkCollectionEditControl)this.editControl).NewLinkCollection;
        }
        catch (Exception exception)
        {
            this.AddErrorValidator(exception.Message);
        }
    }

    public override void CreateDefaultControls()
    {
        base.CopyWebAttributes(this);

        foreach (LinkItem item in this.PropertyLinkCollection)
        {

            Literal literal = new Literal();
            literal.Text = item.ToMappedLink();
            this.Controls.Add(literal);

        }
    }
    Repeater repeater;
    public override void CreateEditControls()
    {
        this.editControl = this.Page.LoadControl(UriSupport.ResolveUrlFromUIBySettings("Edit/PropertyLinkCollectionEditControl.ascx"));

        ((IPropertyLinkCollectionEditControl)this.editControl).OriginalLinkCollection = this.PropertyLinkCollection.Links;
        this.Controls.Add(this.editControl);
        ((IPropertyLinkCollectionEditControl)this.editControl).OriginalLinkCollection = this.PropertyLinkCollection.Links;
        repeater = editControl.FindControl("Links") as Repeater;
        editControl.PreRender += new EventHandler(editControl_PreRender);
    }



    void editControl_PreRender(object sender, EventArgs e)
    {
        var a = ((IPropertyLinkCollectionEditControl)editControl).NewLinkCollection;
        if (repeater.Controls.Count > 0)
        {
            Control header = repeater.Controls[0];

            header.Controls.Clear();
            Literal l = new Literal();
            l.Text = "<table  class=\"epistandardtable\" style=\"width:200px;\">";
            header.Controls.Add(l);
        }
        repeater.Visible = (a.Count > 0);
        editControl.FindControl("AddLinkButton").Visible = !(a.Count > 0);
    }



    // Properties
    private PropertyLinkCollection PropertyLinkCollection
    {
        get
        {
            return (base.PropertyData as PropertyOneLink);
        }
    }
}
Mar 15, 2010

Comments

Gilad Daniel
Gilad Daniel Sep 21, 2010 10:33 AM

Works great !
Our content editors will be pleased. no more 4 property for one link

Jonathan Roberts
Jonathan Roberts May 2, 2014 05:58 PM

Using Episerver 7 can we add in our own fields to the LinkItem popup?

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024