Initializing a control in a custom composite in ASP.NET

Part of the series of controls I'm working on obviously suggests that I put some of them together in composites. I quickly start to find out that this takes into account (this is all new to me!) :)

I basically have a control StyledWindowthat is essentially illustrious Panelwith the ability to make other bits (e.g. add borders, etc.).

Here is the code that creates the child controls in it. Up to this point, it seems to have worked correctly with ordinary static controls:

    protected override void CreateChildControls()
    {
        _panel = new Panel();

        if (_editable != null)
            _editable.InstantiateIn(_panel);

        _regions = new List<IAttributeAccessor>();
        _regions.Add(_panel);
    }

Problems arose today when I tried to put a more complex control into it. This control uses a page link as it introduces JavaScript to make it faster and more responsive (which RegisterClientScriptBlockis the only reason I need a page link).

Now this caused an "object null" error, but I localized it to a rendering method, which, of course, tried to call the method against the [null] object Page.

What bothers me is that the control works fine, as stand-alone, but when it is placed in StyledWindow, everything goes horribly wrong!

So it looks like I'm missing something in mine StyledWindowor ChildControl. Any ideas?

Update

, , Controls. , _panel, , Controls ( -):

    Panel _panel;    // Sub-Control to store the "Content".
    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _panel.Controls;
        }
    }

, . .

Longhorn213

, , . .

, , . , , . OnLoad ! , , , , , - - , ? ? (, , ?!): D

, , :)

+3
4

!

, ! :

  • , Panel , , .
  • - MyCtl.Controls[0].Controls , .
  • , !

, MSDN, artcle (.. "n" - - MSDN ). !

, Panel , , .

:

  • , . Templated Control. , . , .
  • , .
  • , , :)
  • Template .

, ASPX :

<cc1:TemplatedControl ID="MyCtl" runat="server">
    <Template>
        <!-- Templated Content Goes Here -->
    </Template>
</cc1:TemplatedControl>   

,

public class DummyWebControl : WebControl
{
    // Acts as the surrogate for the templated controls.
    // This is essentially the "interface" for the templated data.
}

TemplateControl.cs...

    ITemplate _template;
    // Surrogate to hold the controls instantiated from 
    // within the template.
    DummyWebControl _owner;

    protected override void CreateChildControls()
    {
        // Note we are calling base.Controls here
        // (you will see why in a min).
        base.Controls.Clear();
        _owner = new DummyWebControl();

        // Load the Template Content
        ITemplate template = _template;
        if (template == null)
            template = new StyledWindowDefaultTemplate();
        template.InstantiateIn(_owner);

        base.Controls.Add(_owner);
        ChildControlsCreated = true;
    }

, [Surrogate]:

( / base.Controls)

    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _owner.Controls;
        }
    }

, , !:)

: !

+1

, Controls , , ( ).

+2

JavaScript OnLoad. , .

protected override void OnLoad(EventArgs e)
{

    // Do something to get the script
    string script = GetScript();

    this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "SomeJavaScriptName", script);

    // Could also use this function to determine if the script has been register. i.e. more than 1 of the controls exists
    this.Page.ClientScript.IsClientScriptBlockRegistered("SomeJavaScriptName");

    base.OnLoad(e);
}

, script . , RegisterScriptBlock, script inline .

+1

, , , - , Longhorn , script OnLoad ( ), Brad , , Controls Controls .

, :

  • Controls , Panel Controls, ctl.Controls[0].Controls[0], , . , .
  • Panel Controls, .

So now it works, however, how do I get a property Controlsfor a composite to return elements to Panel, not to Panel?

0
source

Source: https://habr.com/ru/post/1697468/


All Articles