Question

I've got a server control that contains nested server controls,

<uc1:ArticleControl runat="server">
     <HeadLine></HeadLine>
     <Blurb></Blurb>
     <Body></Body>
</uc1:ArticleControl>

Code:

[ToolboxData("<{0}:ArticleControl runat=server></{0}:ArticleControl>")]
[ParseChildren(ChildrenAsProperties = true)]
public class ArticleControl : WebControl
{

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public HeadLineControl HeadLine 
    {
        get;
        set;
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public BlurbControl Blurb
    {
        get;
        set;
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public BodyControl Body
    {
        get;
        set;
    }
}

Nested control definition (applies to all nested controls):

 public class HeadLineControl : ControlBase
     {
          public HeadLineControl() : base() { }
          public HeadLineControl(Article article) : base(article) { }

Base class definition

public abstract class ControlBase : Control
{
     protected Article article;

     protected ControlBase() { }
     protected ControlBase(Article article)
     {
          this.article = article;
     }

The ArticleControl is responsible for writing for the individual parts of the article specified by the nested controls,

My problem is that when the Articlecontrol is created, instances of the nested server controls are created by the .NET framework using the default constructor defined for the System.Web.Ui.Control class eg:

namespace System.Web.UI
{

public class Control : IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
    {
        // Summary:
        //     Initializes a new instance of the System.Web.UI.Control class.
        public Control();

I need to call or override the default behaviour of .Net to call my Control base class constructor in stead of the default .Net defined contructor. So in short, if a new instance of HeadLineControl is created, it needs to created by the ControlBase(Article article) constuctor.

Is this possible and if possible, how do I accomplish this?

Was it helpful?

Solution

I've done this in the meanwhile as a workaround, but there must be a better way?

[PersistenceMode(PersistenceMode.InnerProperty)]
 public HeadLineControl HeadLine 
 {
      get { return null; }
      set 
      {
          this.Controls.Add(new HeadLineControl(articlePage.Article)();
      }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top