Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Anders Hattestad
Mar 15, 2010
  13822
(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 CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog

Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025