November Happy Hour will be moved to Thursday December 5th.

Anders Hattestad
Mar 15, 2010
  13779
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog