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

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

  1. #1

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

    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 Guest

  2. Similar Questions and Discussions

    1. Composite Control And ParseChildren
      Hi, I seem to be missing something with composite controls. I have a tabstrip control that renders ,saves viewstate etc all correctly. I'm using...
    2. using javascript in User controls to access server controls of the user control
      Hello all, I have an asp.net textbox (named txtHidden) and an HtmlButton(named btnAction). I wanted to write a javascript function which will get...
    3. Page_Load called twice in child user control--AutoEventWireup=false
      Hello I have an application which has a user control on an ASPx page. This user control programmatically adds another user control to its Control...
    4. ParseChildren(false) not adding sub objects in the designer
      I have been wrestling with this code for about 2 weeks now. My goal is to create a table for formatting purposes. I want to standardize it as a...
    5. User Server control nested in DataGrid
      Hi all, I am trying to create a web page where im displaying some information in a datagrid and use a nested custom control in EditItemTemplate....
  3. #2

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

    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
    [url]http://staff.develop.com/ballen[/url]
    > 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 Guest

  4. #3

    Default 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
    > [url]http://staff.develop.com/ballen[/url]
    >
    > > 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 Guest

  5. #4

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

    On Thu, 22 Sep 2005 01:33:01 -0700, Karen in England wrote:
    > 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
    >> [url]http://staff.develop.com/ballen[/url]
    >>
    >> > 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
    >>
    >>
    >>
    Not meaning to interrupt your conversation with Brock, I see that you are
    using INamingContainer. Use of this interface may cause different events
    to trigger.

    intrader Guest

  6. #5

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

    On Thu, 22 Sep 2005 18:45:42 +0000, intrader wrote:
    > On Thu, 22 Sep 2005 01:33:01 -0700, Karen in England wrote:
    >
    >> 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
    >>> [url]http://staff.develop.com/ballen[/url]
    >>>
    >>> > 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
    >>>
    >>>
    >>>
    > Not meaning to interrupt your conversation with Brock, I see that you are
    > using INamingContainer. Use of this interface may cause different events
    > to trigger.
    Sorry, missed that you did have the INamingContainer.
    I hope that Brock will explain to us further


    intrader Guest

  7. #6

    Default 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
    [url]http://staff.develop.com/ballen[/url]


    Brock Allen Guest

  8. #7

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

    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 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