Try our conversational search powered by Generative AI!

Anders Hattestad
Mar 15, 2010
  13646
(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
The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024

What's new in Language Manager 5.3.0

In Language Manager (LM) version 5.2.0, we added an option in appsettings.json called TranslateOrCopyContentAreaChildrenBlockForTypes . It does...

Quoc Anh Nguyen | Apr 15, 2024

Optimizely Search & Navigation: Boosting in Unified Search

In the Optimizely Search & Navigation admin view, administrators can set a certain weight of different properties (title, content, summary, or...

Tung Tran | Apr 15, 2024