locked
Controls of Template not available of a Templated Control RRS feed

  • Question

  • User584511489 posted

    Hello,

    I have created an easy custom templated control with a TemplateInstance.Single template.

    I have this control added into a panel of the page. First, the panel is not visible.

    If you click a button outside, the panel becomes visible and then, I want to access the textbox within the template.

    But the problem is, at this moment, the textbox is null. The textbox is not created, I think, because the panel was invisible, the control was not rendered, and therefore, the CreateChildControls methos was not called.

    If i access the controls-collection of my templated control, a codeline before, now this access causes that the CreateChildControls method is called and now I can access the textbox within the template.

    I have also tried to call the EnsureChildControls method one line before, but this did not work.

    What can I do, that the child controls will be created, also, the container was not visible and I do not want to access the controls-collection?

    Does anyone has an idea?

    Thanks,

    Harald



    Thursday, September 24, 2009 4:00 AM

Answers

  • User584511489 posted

    Hi,

    now, I have found the solution for the problem. Because, the controls are not created yet, I have to create the child controls in the OnInit Event:

    I have added the following procedure:


    protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                EnsureChildControls();
            }


    now I do not want to access the controls collection everything is working properly.


    kind regards,

    Harald

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 26, 2009 4:11 AM

All replies

  • User-861155893 posted

    Please post source code...

    Thursday, September 24, 2009 7:00 AM
  • User584511489 posted

    Simplyfied Sourcecode:


    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        [ParseChildren(true)] //wenn ich hier false wähle, wird mir das ContentTemplate nicht instanziert
        [PersistChildren(false)]
        [Browsable(true)]
        [ToolboxData("<{0}:VarBoxContentControl  runat=\"server\"><ContentTemplate></ContentTemplate></{0}:VarBoxContentControl>")]
        [Designer("VarBoxContentDesigner")]
        public class VarBoxContentControl : CompositeControl
        {

            private HtmlGenericControl divBox;
            private VarBoxContainer varBoxContainer;


            private ITemplate _contentTemplate = null;


      

            public VarBoxContentControl() { }


            [PersistenceMode(PersistenceMode.InnerProperty),
             TemplateContainer(typeof(VarBoxContentControl)),
            TemplateInstance(TemplateInstance.Single)]
            [MergableProperty(false)]
            [DefaultValue(null)]
            [Browsable(false)]
            public ITemplate ContentTemplate
            {
                get
                {
                    return _contentTemplate;
                }
                set
                {
                    _contentTemplate = value;
                }
            }




            protected override void CreateChildControls()
            {
                Controls.Clear();
                base.CreateChildControls();

                divBox = new HtmlGenericControl("div");

                varBoxContainer = new VarBoxContainer();
                varBoxContainer.ID = "boxContent";

                divBox.Controls.Add(varBoxContainer);

                if (ContentTemplate != null)
                {
                    ContentTemplate.InstantiateIn(varBoxContainer);
                }
                Controls.Add(divBox);
                ChildControlsCreated = true;
            }

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

            protected override void Render(HtmlTextWriter writer)
            {
                EnsureChildControls();
                try
                {
                    divBox.Attributes.Add("id", "box");
                    divBox.Attributes.Add("class", "varBox varBoxRight");
                    divBox.Attributes.Add("runat", "server");
                    divBox.Attributes.Add("style", "margin-bottom:15px;");

                    varBoxContainer.Attributes.Add("style", "margin-top:10px;margin-left:20px;margin-right:20px;padding-bottom :20px;");

                    divBox.RenderControl(writer);
                }
                catch (Exception ex)
                {

                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }

             public override void DataBind()
            {
                EnsureChildControls();
                base.DataBind();
            }
        }



        [ToolboxItem(false)]
        public class VarBoxContainer : Panel, INamingContainer
        {

        }


        public class VarBoxContentDesigner : ContainerControlDesigner
        {
            public override void Initialize(IComponent component)
            {
                base.Initialize(component);
                SetViewFlags(ViewFlags.TemplateEditing, true);
            }

            public override TemplateGroupCollection TemplateGroups
            {
                get
                {
                    TemplateGroupCollection templateGroups = new TemplateGroupCollection();
                    VarBoxContentControl controlDesigner = (VarBoxContentControl)this.Component;
                    TemplateGroup group = new TemplateGroup("Item");
                    TemplateDefinition template = new TemplateDefinition(this, "Template", controlDesigner, "ContentTemplate", false);
                    group.AddTemplateDefinition(template);
                    templateGroups.Add(group);
                    return templateGroups;
                }
            }


        }


    I have added a control the the page:

    <mCrtl:VarBoxContentControl ID="mbcCourse" runat="server">
                 <ContentTemplate>
                            <asp:TextBox runat="server" id="tbContent" />
                  </ContentTemplate>
    </mCrtl:VarBoxContentControl>          


    If I want to access from outside, I have to use the following code:

    ControlCollection colDummy = mbcCourse.Controls;

    string enteredText = tbContent.Text;


    If I do not access the controls-Collection, tbContent is empty.



    Thursday, September 24, 2009 7:13 AM
  • User-2106054853 posted

     Hi,

    You cannot access the control in the Template by its ID directly. Please think of how you get the control's reference in a GridView. It's the same for your own control.

    Friday, September 25, 2009 4:54 AM
  • User584511489 posted

    Hi,

    sure, you can access the controls of the template directly, if you have added the attribute

    [TemplateInstance(TemplateInstance.Single)]

    It is the duty of this attribute to afford this feature. You do not need to use the FindControl-Method.
    But I have also tried the FindControl method and it is the same problem.

    You can only find after you have accessed the controls-Collection.

    kind regards,

    Harald

    Friday, September 25, 2009 6:56 AM
  • User584511489 posted

    Hi,

    now, I have found the solution for the problem. Because, the controls are not created yet, I have to create the child controls in the OnInit Event:

    I have added the following procedure:


    protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                EnsureChildControls();
            }


    now I do not want to access the controls collection everything is working properly.


    kind regards,

    Harald

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 26, 2009 4:11 AM