Recommended way to add child controls in CreateChildControls

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Recommended way to add child controls in CreateChildControls

    Hi all,

    What is the recommended way to add child controls *around* existing
    child controls in the CreateChildControls method (e.g. table, row and
    cell controls for layout purposes)?

    Is the best way to keep a reference to the existing child controls,
    clear the Controls collection and then re-add everything along with the
    layout controls, or insert the layout controls around the existing
    controls in the existing Controls collection?

    Thanks,

    Paul

    paul.hester@gmail.com Guest

  2. Similar Questions and Discussions

    1. Control.Controls bug? Control's child controls missing at the run time.
      Hello, ..NET 1.1/VB.NET: I have a custom web control Public Class DatePicker Inherits Control Implements INamingContainer
    2. Question about Controls collection, CreateChildControls and Render methods
      Hi, Consider this code (simplified) example: public class Person : Microsoft.SharePoint.WebPartPages.WebPart { private ImageButton _action =...
    3. HELP! Child controls of child controls not visible to web app
      I have a custom server control. It's a TabControl. It's really cool, actually. Looks just like a standard VB6 TabControl. It lets you set the...
    4. user controls: dynamiclly added child controls dont survive post back ?
      hi, i have some strange behaviour: i've created a web user control that add's some child controls (e.g: textbox, image buttons) to its control...
    5. Adding Literal Controls in CreateChildControls - Is this bad?
      Adding Literal Controls in CreateChildControls - Is this bad? Someone told me it was but every example I've seen does not have a problem doing...
  3. #2

    Default Re: Recommended way to add child controls in CreateChildControls

    If I try to modify the Controls collection, I get a "The Controls
    collection cannot be modified because the control contains code blocks"
    error because my controls contain literal content (i.e. <%=
    SomeValue%>). If I change the literal content to a <%# %> tag it
    doesn't get picked up and is rendered empty.

    Is there any way to insert layout controls around the existing controls
    and preserve the literal content?

    Any help would be appreciated. I'm stuck on this one.

    Thanks,

    Paul

    [email]paul.hester@gmail.com[/email] wrote:
    > Hi all,
    >
    > What is the recommended way to add child controls *around* existing
    > child controls in the CreateChildControls method (e.g. table, row and
    > cell controls for layout purposes)?
    >
    > Is the best way to keep a reference to the existing child controls,
    > clear the Controls collection and then re-add everything along with the
    > layout controls, or insert the layout controls around the existing
    > controls in the existing Controls collection?
    >
    > Thanks,
    >
    > Paul
    paul.hester@gmail.com Guest

  4. #3

    Default Re: Recommended way to add child controls in CreateChildControls

    Paul, in resposne to your previous post, i have already explained to you why
    that is happening and how to do it properly if you want to use inline code
    block expression syntax nested within your custom control.
    Let me know what about it did not convince you. Also you keep posting in two
    different groups simultaneously, and it will only become hard for you to
    keep track.

    [url]http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontro ls/browse_thread/thread/c9f142ee693fd3d0/9526ae015a40888f?lnk=st&q=&rnum=1&hl=en[/url]

    Regards,
    Alessandro Zifiglio
    [url]http://www.AsyncUI.net[/url]

    <paul.hester@gmail.com> ha scritto nel messaggio
    news:1154583151.823542.121740@p79g2000cwp.googlegr oups.com...
    > Hi all,
    >
    > What is the recommended way to add child controls *around* existing
    > child controls in the CreateChildControls method (e.g. table, row and
    > cell controls for layout purposes)?
    >
    > Is the best way to keep a reference to the existing child controls,
    > clear the Controls collection and then re-add everything along with the
    > layout controls, or insert the layout controls around the existing
    > controls in the existing Controls collection?
    >
    > Thanks,
    >
    > Paul
    >

    Alessandro Zifiglio Guest

  5. #4

    Default Re: Recommended way to add child controls in CreateChildControls

    Hi Alessandro,

    Thanks for your reply, and sorry about the multiple posts - I've been
    getting different responses in different groups.

    I haven't tried your example yet because I couldn't see how to add the
    layout elements between the child controls. I need to have table, tr
    and td elements around the controls and have certain controls grouped
    together in a td (e.g. all validator objects in the same td). Also,
    certain custom attributes will determine the layout too. I could just
    have these elements in the aspx markup file, but I'm going to be using
    this control all over my site and want to avoid the code repetition.

    I hope I've made this clear. Any help would be appreciated.

    Thanks,

    Paul

    Alessandro Zifiglio wrote:
    > Paul, in resposne to your previous post, i have already explained to you why
    > that is happening and how to do it properly if you want to use inline code
    > block expression syntax nested within your custom control.
    > Let me know what about it did not convince you. Also you keep posting in two
    > different groups simultaneously, and it will only become hard for you to
    > keep track.
    >
    > [url]http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontro ls/browse_thread/thread/c9f142ee693fd3d0/9526ae015a40888f?lnk=st&q=&rnum=1&hl=en[/url]
    >
    > Regards,
    > Alessandro Zifiglio
    > [url]http://www.AsyncUI.net[/url]
    >
    > <paul.hester@gmail.com> ha scritto nel messaggio
    > news:1154583151.823542.121740@p79g2000cwp.googlegr oups.com...
    > > Hi all,
    > >
    > > What is the recommended way to add child controls *around* existing
    > > child controls in the CreateChildControls method (e.g. table, row and
    > > cell controls for layout purposes)?
    > >
    > > Is the best way to keep a reference to the existing child controls,
    > > clear the Controls collection and then re-add everything along with the
    > > layout controls, or insert the layout controls around the existing
    > > controls in the existing Controls collection?
    > >
    > > Thanks,
    > >
    > > Paul
    > >
    paul.hester@gmail.com Guest

  6. #5

    Default Re: Recommended way to add child controls in CreateChildControls

    Paul, you may try exposing a couple of templates. That is the alternative
    route and you will get more flexibility in what you can do. Grouping your
    controls within templates is much better. Just make sure that the validator
    controls are grouped together with the fields they validate, either within
    the same template or in different templates but within the same
    NamingContainer.

    You will also need to expose an itemcreated event with an argument that
    gives you a handle on the template that contains the control or something
    like that which you can use to retrieve the controls contained within your
    templates. You wont be able to access them directly.

    Another thing to be aware of is controls nested within your templates that
    fire events, for these also you might want to override OnBubbleEvent in the
    template and expose another generic method that fires for all click events
    etc. You might have noticed this when using datacontrols like datalist,
    repeater or datagrid controls, who expose ItemCreated and ItemCommand(for
    events).

    I wanted to include complete working code but it gets too involved and i'm
    not sure this is the route you want to take anyway, but its a very common
    topic and there are many examples by many bloggers or msdn itself.
    Here is a simplistic example on msdn :
    [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.itemplate.aspx[/url]

    you should be able to take it from there, if that is what you want to do.
    Following also is a simple example i wrote based on what you said here, i
    hope it helps you.

    Regards,
    Alessandro Zifiglio
    [url]http://www.AsyncUI.net[/url]

    <%@ Page Language="C#" %>

    <%@ Register Assembly="AsyncUI" Namespace="AsyncUI.TestProjects"
    TagPrefix="cc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    string blah = "paul";
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <cc1:MyWebCustomControl ID="MyWebCustomControl1" runat="server">

    <LabelsTemplate>
    <asp:Label ID="Label1" runat="server"
    Text="Label1"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server"
    Text="Label2"></asp:Label>
    </LabelsTemplate>

    <FormFieldTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </FormFieldTemplate>

    <ValidatorTemplate>
    <span style="color:Green;font-weight:bold"><%= blah
    %></span>
    <asp:RequiredFieldValidator Display="Dynamic"
    ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server"
    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator Display="Dynamic"
    ControlToValidate="TextBox2" ID="RequiredFieldValidator2" runat="server"
    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    </ValidatorTemplate>

    </cc1:MyWebCustomControl>

    <asp:Button ID="Button1" runat="server" Text="Submit" />
    </div>
    </form>
    </body>
    </html>

    //////////////////////////////////////////////
    ////// Custom control
    //////////////////////////////////////////////////
    using System;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.ComponentModel;
    using System.Web.UI;
    namespace ControlTestCenter
    {

    public class MyWebCustomControl : WebControl
    {
    private ITemplate[] templates;
    public MyWebCustomControl()
    : base("table")
    {
    //Initialization code here.
    // we want to expose 3 template
    templates = new ITemplate[3];
    }

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
    base.AddAttributesToRender(writer);
    writer.AddStyleAttribute(HtmlTextWriterStyle.Width , "100%");
    writer.AddStyleAttribute(HtmlTextWriterStyle.Verti calAlign,
    "top");
    }

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplateItem))]
    public ITemplate FormFieldTemplate
    {
    get
    {
    return templates[0];
    }
    set
    {
    templates[0] = value;
    }
    }

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplateItem))]
    public ITemplate LabelsTemplate
    {
    get
    {
    return templates[1];
    }
    set
    {
    templates[1] = value;
    }
    }

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplateItem))]
    public ITemplate ValidatorTemplate
    {
    get
    {
    return templates[2];
    }
    set
    {
    templates[2] = value;
    }
    }

    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    // use it to get to the naming Container
    TemplateItem ti = new TemplateItem();
    if (LabelsTemplate != null)
    {
    LabelsTemplate.InstantiateIn(ti.Cells[0]);
    }
    if (FormFieldTemplate != null)
    {
    FormFieldTemplate.InstantiateIn(ti.Cells[1]);
    }
    if (ValidatorTemplate != null)
    {
    ValidatorTemplate.InstantiateIn(ti.Cells[2]);
    }
    Controls.Add(ti);
    }
    }
    public enum TemplateType
    {
    FormFields,
    Labels,
    Validator
    }
    [ToolboxItem(false)]
    public class TemplateItem : TableRow, INamingContainer
    {
    private TemplateType itemTypeValue;

    public TemplateItem()
    {
    Cells.Add(new TableCell());// labels
    Cells.Add(new TableCell()); // formFields
    Cells.Add(new TableCell()); // validators
    }

    public TemplateItem(TemplateType itemType)
    {
    this.itemTypeValue = itemType;
    Cells.Add(new TableCell());
    }


    public virtual TemplateType ItemType
    {
    get
    {
    return itemTypeValue;
    }
    }

    }
    }



    <paul.hester@gmail.com> ha scritto nel messaggio
    news:1154647453.916665.110080@m73g2000cwd.googlegr oups.com...
    > Hi Alessandro,
    >
    > Thanks for your reply, and sorry about the multiple posts - I've been
    > getting different responses in different groups.
    >
    > I haven't tried your example yet because I couldn't see how to add the
    > layout elements between the child controls. I need to have table, tr
    > and td elements around the controls and have certain controls grouped
    > together in a td (e.g. all validator objects in the same td). Also,
    > certain custom attributes will determine the layout too. I could just
    > have these elements in the aspx markup file, but I'm going to be using
    > this control all over my site and want to avoid the code repetition.
    >
    > I hope I've made this clear. Any help would be appreciated.
    >
    > Thanks,
    >
    > Paul
    >
    > Alessandro Zifiglio wrote:
    >> Paul, in resposne to your previous post, i have already explained to you
    >> why
    >> that is happening and how to do it properly if you want to use inline
    >> code
    >> block expression syntax nested within your custom control.
    >> Let me know what about it did not convince you. Also you keep posting in
    >> two
    >> different groups simultaneously, and it will only become hard for you to
    >> keep track.
    >>
    >> [url]http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontro ls/browse_thread/thread/c9f142ee693fd3d0/9526ae015a40888f?lnk=st&q=&rnum=1&hl=en[/url]
    >>
    >> Regards,
    >> Alessandro Zifiglio
    >> [url]http://www.AsyncUI.net[/url]
    >>
    >> <paul.hester@gmail.com> ha scritto nel messaggio
    >> news:1154583151.823542.121740@p79g2000cwp.googlegr oups.com...
    >> > Hi all,
    >> >
    >> > What is the recommended way to add child controls *around* existing
    >> > child controls in the CreateChildControls method (e.g. table, row and
    >> > cell controls for layout purposes)?
    >> >
    >> > Is the best way to keep a reference to the existing child controls,
    >> > clear the Controls collection and then re-add everything along with the
    >> > layout controls, or insert the layout controls around the existing
    >> > controls in the existing Controls collection?
    >> >
    >> > Thanks,
    >> >
    >> > Paul
    >> >
    >

    Alessandro Zifiglio Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139