Try our conversational search powered by Generative AI!

Anders Hattestad
Mar 15, 2010
  13650
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog