0

I may have been out of Web Forms for two long. I need to implement dynamic placeholders for web forms. I am trying to use Fortis' version of it, but it has very little documentation around it. The MVC side works great.

Can someone give me some information on how to implement it with web forms?

1

2 Answers 2

5

It is possible to use the Fortis Dyanmic Placeholders in Web Forms however you'll need to create your own method of generating the placeholder ID.

The MVC implementation is as follows:

public static HtmlString DynamicPlaceholder(this SitecoreHelper helper, string placeholderName)
{
    var placeholder = PlaceholdersContext.Add(placeholderName, RenderingContext.Current.Rendering.UniqueId);

    return helper.Placeholder(placeholder);
}

True Clarity wrote a blog post on how to implement a dynamic placeholder control for Web Forms. Below is an adapted version that should work with the Fortis Dynamic Placeholder library.

public class DynamicPlaceholder : WebControl, IExpandable
{
    private string key = Placeholder.DefaultPlaceholderKey;

    public string Key
    {
        get { return _key; }
        set { _key = value; }
    }

    protected override void CreateChildControls()
    {
        var parentRendering = this.Parent as Sublayout;

        if (parentRendering == null)
        {
            // Problem - parent control isn't a Sublayout (why?)
        }

        var renderingId = parentRendering.ID
        var dynamicKey = PlaceholdersContext.Add(placeholderName, renderingId);
        var placeholder = new Placeholder();

        placeholder.Key = dynamicKey;

        this.Controls.Add(placeholder);
        placeholder.Expand();
    }

    protected override void DoRender(HtmlTextWriter output)
    {
        base.RenderChildren(output);
    }

    public void Expand()
    {
        this.EnsureChildControls();
    }
}

Please note that I haven't tested the above at all and I've just written it :). It's likely it may need tweaking but I think the general approach is there. I hope this helps!

2
  • You and I went down the same path. Let me try yours to see if it works better. I am doing the same as you, but using the Dynamic key you mentioned. They may just work the same. Thanks Jason.
    – Chris Auer
    Commented Jan 5, 2017 at 3:57
  • Yeah I think the key (no pun intended :)) difference is the logic for generating the dynamic key is abstracted. I've seen others wanting to specify a dynamic key or like the True Clarity example increment a number. Perhaps an enhancement for the library to allow this :). As the generation of the dynamic key is abstract this can be overridden.
    – Jason Bert
    Commented Jan 5, 2017 at 10:14
1

I did something similar to what Jason mentioned to get it working. I created a project with Unicorn items and working examples. You can find that project here. https://github.com/dnstommy/DynamicPlaceholders.Webforms/

The code that worked for me is as follows. It uses Dynamic keys for the placeholder keys and Fortis for all the saving and compitable rending switching.

public class PlaceholderControl : WebControl, IExpandable
{
    private string _key = Placeholder.DefaultPlaceholderKey;
    private Placeholder _placeholder;

    public string Key
    {
        get
        {
            return _key;
        }
        set
        {
            _key = value.ToLower();
        }
    }

    protected string DynamicKey
    {
        get
        {
            if (DynamicKey1 != null)
            {
                return DynamicKey1;
            }
            DynamicKey1 = _key;
            var stack = Switcher<Placeholder, PlaceholderSwitcher>.GetStack(false);
            var current = stack.Peek();
            var renderings = Sitecore.Context.Page.Renderings.Where(rendering => (rendering.Placeholder == current.ContextKey || rendering.Placeholder == current.Key) && rendering.AddedToPage);
            var renderingReferences = renderings as RenderingReference[] ?? renderings.ToArray();
            if (!renderingReferences.Any()) return DynamicKey1;
            {
                var rendering = renderingReferences.Last();
                DynamicKey1 = rendering.UniqueId;
            }
            return DynamicKey1;
        }
    }

    public string DynamicKey1 { get; set; }

    protected override void CreateChildControls()
    {
        Sitecore.Diagnostics.Tracer.Debug("DynamicKeyPlaceholder: Adding dynamic placeholder with Key " + DynamicKey);
        _placeholder = new Placeholder { Key = PlaceholdersContext.Add(Key, Guid.Parse(DynamicKey)) };
        Controls.Add(_placeholder);
        _placeholder.Expand();
    }

    protected override void DoRender(HtmlTextWriter output)
    {
        RenderChildren(output);
    }

    public void Expand()
    {
        EnsureChildControls();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.