tags:

views:

289

answers:

1

Hello there,

I have a server control, inheriting from PlaceHolder.

Basically, all it is, is a placeholder, with the top part having a "<div class etc...", and the bottom closing it off.

So, typical usage would be

<control:control runat="server" id="phControl">
   <asp:TextBox runat="server" id="txtControl">
   <asp:DropDownList runat="server"id="ddlControl">
</control:control>

or something similar.

It has struck me that if I postback to the control, it loses all the items in the ddlControl (or whatever), and that implementing IPostBackHandler apparently would solve all my woes.

I had a quick glance through the documentation, but am still not really sure what I am implementing (obviously I have the method names, but I don't really get what is expected in here)

Any pointers in the right direction would be greatly appreciated.

Thanks,

Tim

+1  A: 

Hi,

Its looks like you just want a server control that can contains other controls or a "template", I have just done this using the example at: http://msdn.microsoft.com/en-us/library/ms178657.aspx

This should handle all the work done on postback.

A basic example adapted from the above link:

using System; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.Design; using System.Web.UI.WebControls;

namespace Made4Print.Web.UI { [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), Designer(typeof(VacationHomeDesigner)), DefaultProperty("Title"), ToolboxData("<{0}:TemplateContainer runat=\"server\"> "),] public class TemplateContainer : CompositeControl { private ITemplate templateValue; private TemplateOwner ownerValue;

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public TemplateOwner Owner
    {
        get
        {
            return ownerValue;
        }
    }

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(typeof(ITemplate), ""), Description("Control template"), TemplateContainer(typeof(TemplateContainer))]
    public virtual ITemplate Template
    {
        get
        {
            return templateValue;
        }
        set
        {
            templateValue = value;
        }
    }

    protected override void CreateChildControls()
    {
        Controls.Clear();
        ownerValue = new TemplateOwner();

        ITemplate temp = templateValue;
        if (temp == null)
        {
            temp = new DefaultTemplate();
        }

        temp.InstantiateIn(ownerValue);
        this.Controls.Add(ownerValue);
    }

    public override void DataBind()
    {
        CreateChildControls();
        ChildControlsCreated = true;
        base.DataBind();
    }

}

[ToolboxItem(false)]
public class TemplateOwner : WebControl
{
}

#region DefaultTemplate
sealed class DefaultTemplate : ITemplate
{
    void ITemplate.InstantiateIn(Control owner)
    {
        // Create Controls Here
        //Label title = new Label();
        //title.DataBinding += new EventHandler(title_DataBinding);
        //owner.Controls.Add(title);
    }

    //void title_DataBinding(object sender, EventArgs e)
    //{
    //    Label source = (Label)sender;
    //    TemplateContainer container = (TemplateContainer)(source.NamingContainer);
    //    source.Text = container.Title;
    //}
}
#endregion


public class VacationHomeDesigner : ControlDesigner
{

    public override void Initialize(IComponent Component)
    {
        base.Initialize(Component);
        SetViewFlags(ViewFlags.TemplateEditing, true);
    }

    public override string GetDesignTimeHtml()
    {
        return "<span>[Template Container Control]</span>";
    }

    public override TemplateGroupCollection TemplateGroups
    {
        get
        {
            TemplateGroupCollection collection = new TemplateGroupCollection();
            TemplateGroup group;
            TemplateDefinition template;
            TemplateContainer control;

            control = (TemplateContainer)Component;
            group = new TemplateGroup("Item");
            template = new TemplateDefinition(this, "Template", control, "Template", true);
            group.AddTemplateDefinition(template);
            collection.Add(group);
            return collection;
        }
    }


}

}

Mark Redman