Nested controls in User Control via ParseChildren(false) not worki

Discussion in 'ASP .Net Building Controls' started by Karen in England, Sep 21, 2005.

  1. Hello,

    Using ASP.NET 2.0/Visual Studio 2005 Beta 2 and programming in C#, I'm
    trying to create a user control that allows nested content between its start
    and end tags. From googling around I got the impression that the
    ParseChildren(false) attribute was what I needed, so my code looks like this:

    [ParseChildren(false)]
    public partial class Box : System.Web.UI.UserControl, INamingContainer {
    protected override void Render(HtmlTextWriter output) {
    // irrelevant code deleted
    RenderChildren(output);
    // irrelevant code deleted
    }
    }

    I can add this control to a page, and it even does what I want when I test
    the page in a browser. The only trouble is, in the Visual Studio designer I
    get the error message "Type 'System.Web.UI.UserControl' does not have a
    public property named '<name of whatever control I try to use>'". I also
    experimented with the PersistChildren attribute to no avail. Am I doing
    something wrong, or is this just not supposed to work with user controls?

    I tried the same thing with a web control, and it worked just fine in the
    designer:

    [ToolboxData("<{0}:Box runat=server></{0}:Box>")]
    [ParseChildren(false)]
    public class Box : WebControl, INamingContainer {
    protected override void Render(HtmlTextWriter output) {
    // irrelevant code deleted
    RenderChildren(output);
    // irrelevant code deleted
    }
    }

    Thanks in advance for any responses,
    Karen
     
    Karen in England, Sep 21, 2005
    #1
    1. Advertising

  2. Karen in England

    Brock Allen Guest

    UserControls can't have nested content as defined by the page, since the
    ASCX already contains child controls as defined by the ASCX. I think you
    just mean a custom control. For custom controls [ParseChildren(false)] tells
    the parser that nested content is not meant to be child controls, but to
    be property assignments. This is why you get the error. Change it to ParseChildren(true).
    The parser will then allow child controls and they will automatically be
    added into your control's Controls collection. And then, yes, make sure in
    Render() you call base.Render() which will render the child controls.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > Hello,
    >
    > Using ASP.NET 2.0/Visual Studio 2005 Beta 2 and programming in C#, I'm
    > trying to create a user control that allows nested content between its
    > start and end tags. From googling around I got the impression that
    > the ParseChildren(false) attribute was what I needed, so my code looks
    > like this:
    >
    > [ParseChildren(false)]
    > public partial class Box : System.Web.UI.UserControl, INamingContainer
    > {
    > protected override void Render(HtmlTextWriter output) {
    > // irrelevant code deleted
    > RenderChildren(output);
    > // irrelevant code deleted
    > }
    > }
    > I can add this control to a page, and it even does what I want when I
    > test the page in a browser. The only trouble is, in the Visual Studio
    > designer I get the error message "Type 'System.Web.UI.UserControl'
    > does not have a public property named '<name of whatever control I try
    > to use>'". I also experimented with the PersistChildren attribute to
    > no avail. Am I doing something wrong, or is this just not supposed to
    > work with user controls?
    >
    > I tried the same thing with a web control, and it worked just fine in
    > the designer:
    >
    > [ToolboxData("<{0}:Box runat=server></{0}:Box>")]
    > [ParseChildren(false)]
    > public class Box : WebControl, INamingContainer {
    > protected override void Render(HtmlTextWriter output) {
    > // irrelevant code deleted
    > RenderChildren(output);
    > // irrelevant code deleted
    > }
    > }
    > Thanks in advance for any responses,
    > Kare
     
    Brock Allen, Sep 21, 2005
    #2
    1. Advertising

  3. Re: Nested controls in User Control via ParseChildren(false) not w

    Hi Brock,

    Thank you very much for your reply. You're correct that I don't have
    problems when I write a custom control (subclass of WebControl). I'm just
    confused about the following: The name of the argument to the ParseChildren
    attribute is "ChildrenAsProperties", so is it possible you're got true and
    false backwards in your explanation?

    Also, I put together a small test project to test your theory that I
    shouldn't be able to get stuff into a user control's control collection by
    adding content between its start and end tags. Here's my code (the user
    control in this case is completely useless--I was just trying to keep the
    example simple):

    Test.ascx.cs (The code in Page_Load is just to inspect the contents of the
    control collection, whilst the Render method is what's simulating the effect
    I want):

    using System;
    using System.Web.UI;

    [ParseChildren(false)]
    public partial class Test : System.Web.UI.UserControl, INamingContainer {
    protected void Page_Load(object sender, EventArgs e) {
    IterateThroughControls(Controls);
    }

    private void IterateThroughControls(ControlCollection controls) {
    foreach (Control control in controls) {
    Response.Write(control.GetType().Name + "<br />");
    IterateThroughControls(control.Controls);
    }
    }

    protected override void Render(HtmlTextWriter output) {
    output.WriteFullBeginTag("table");
    output.WriteFullBeginTag("tr");
    output.WriteFullBeginTag("td");
    RenderChildren(output);
    output.WriteEndTag("td");
    output.WriteEndTag("tr");
    output.WriteEndTag("table");
    }
    }

    Default.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>
    <%@ Register Src="Test.ascx" TagName="Test" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <uc1:Test ID="Test1" runat="server">
    <asp:Label ID="Karen" runat="server">Hello World</asp:Label>
    </uc1:Test>
    </div>
    </form>
    </body>
    </html>

    Default.aspx showed up in my browser when I tested as:

    LiteralControl
    Label
    LiteralControl


    Hello World


    So, it appears that the Label was successfully added to the user control's
    control collection. In fact, everything is working as I would have expected
    except that I get the error message mentioned in my original post when I try
    to view Default.aspx in design mode. What I'm wondering then, is whether
    I've left something out that's necessary for designer support, or whether
    this is just missing functionality in VS? I am, after all, working with a
    beta...

    Thanks,
    Karen

    "Brock Allen" wrote:

    > UserControls can't have nested content as defined by the page, since the
    > ASCX already contains child controls as defined by the ASCX. I think you
    > just mean a custom control. For custom controls [ParseChildren(false)] tells
    > the parser that nested content is not meant to be child controls, but to
    > be property assignments. This is why you get the error. Change it to ParseChildren(true).
    > The parser will then allow child controls and they will automatically be
    > added into your control's Controls collection. And then, yes, make sure in
    > Render() you call base.Render() which will render the child controls.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    > > Hello,
    > >
    > > Using ASP.NET 2.0/Visual Studio 2005 Beta 2 and programming in C#, I'm
    > > trying to create a user control that allows nested content between its
    > > start and end tags. From googling around I got the impression that
    > > the ParseChildren(false) attribute was what I needed, so my code looks
    > > like this:
    > >
    > > [ParseChildren(false)]
    > > public partial class Box : System.Web.UI.UserControl, INamingContainer
    > > {
    > > protected override void Render(HtmlTextWriter output) {
    > > // irrelevant code deleted
    > > RenderChildren(output);
    > > // irrelevant code deleted
    > > }
    > > }
    > > I can add this control to a page, and it even does what I want when I
    > > test the page in a browser. The only trouble is, in the Visual Studio
    > > designer I get the error message "Type 'System.Web.UI.UserControl'
    > > does not have a public property named '<name of whatever control I try
    > > to use>'". I also experimented with the PersistChildren attribute to
    > > no avail. Am I doing something wrong, or is this just not supposed to
    > > work with user controls?
    > >
    > > I tried the same thing with a web control, and it worked just fine in
    > > the designer:
    > >
    > > [ToolboxData("<{0}:Box runat=server></{0}:Box>")]
    > > [ParseChildren(false)]
    > > public class Box : WebControl, INamingContainer {
    > > protected override void Render(HtmlTextWriter output) {
    > > // irrelevant code deleted
    > > RenderChildren(output);
    > > // irrelevant code deleted
    > > }
    > > }
    > > Thanks in advance for any responses,
    > > Karen

    >
    >
    >
     
    Karen in England, Sep 22, 2005
    #3
  4. Karen in England

    Brock Allen Guest

    Re: Nested controls in User Control via ParseChildren(false) not w

    > Thank you very much for your reply. You're correct that I don't have
    > problems when I write a custom control (subclass of WebControl). I'm
    > just confused about the following: The name of the argument to the
    > ParseChildren attribute is "ChildrenAsProperties", so is it possible
    > you're got true and false backwards in your explanation?


    You're absolutely right -- sry about that. I always get it confused with
    PersistChildren(false). So yes, it should read ParseChildren(true).

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen
     
    Brock Allen, Sep 22, 2005
    #4
  5. Karen in England

    Wes Guest

    I have the exact same issue. Did you ever find a resolution?

    "Karen in England" wrote:

    > Hello,
    >
    > Using ASP.NET 2.0/Visual Studio 2005 Beta 2 and programming in C#, I'm
    > trying to create a user control that allows nested content between its start
    > and end tags. From googling around I got the impression that the
    > ParseChildren(false) attribute was what I needed, so my code looks like this:
    >
    > [ParseChildren(false)]
    > public partial class Box : System.Web.UI.UserControl, INamingContainer {
    > protected override void Render(HtmlTextWriter output) {
    > // irrelevant code deleted
    > RenderChildren(output);
    > // irrelevant code deleted
    > }
    > }
    >
    > I can add this control to a page, and it even does what I want when I test
    > the page in a browser. The only trouble is, in the Visual Studio designer I
    > get the error message "Type 'System.Web.UI.UserControl' does not have a
    > public property named '<name of whatever control I try to use>'". I also
    > experimented with the PersistChildren attribute to no avail. Am I doing
    > something wrong, or is this just not supposed to work with user controls?
    >
    > I tried the same thing with a web control, and it worked just fine in the
    > designer:
    >
    > [ToolboxData("<{0}:Box runat=server></{0}:Box>")]
    > [ParseChildren(false)]
    > public class Box : WebControl, INamingContainer {
    > protected override void Render(HtmlTextWriter output) {
    > // irrelevant code deleted
    > RenderChildren(output);
    > // irrelevant code deleted
    > }
    > }
    >
    > Thanks in advance for any responses,
    > Karen
     
    Wes, Nov 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Fields
    Replies:
    1
    Views:
    3,322
    RadekP
    Jan 28, 2004
  2. Sam Fields

    ParseChildren(false) not adding sub objects in the designer

    Sam Fields, Jan 28, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    212
    Sam Fields
    Jan 29, 2004
  3. Guest

    RenderChildren/ParseChildren

    Guest, Mar 1, 2005, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    248
    Steven Cheng[MSFT]
    Mar 16, 2005
  4. John Spittle

    Composite Control And ParseChildren

    John Spittle, Apr 28, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    166
    John Spittle
    Apr 28, 2006
  5. Mariusz

    multiple ParseChildren

    Mariusz, Dec 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    365
    Smokejumper
    Dec 23, 2004
Loading...

Share This Page