Removing a child control in event it fires

Discussion in 'ASP .Net' started by =?Utf-8?B?SmVmZiBFdmFucw==?=, Nov 19, 2004.

  1. Hi,

    I was wondering if it's possible to remove a Control c from a Controls
    collection in the parent's event handler for an event fired by c.

    For instance let's say we have

    public class ChildControl : Control
    {
    public event EventHandler WorkDone(object sender, EventArgs e);
    }

    public class ParentControl : Control
    {
    protected override void OnLoad(EventArgs e)
    {
    ChildControl c = new ChildControl();
    c.WorkDone += new EventHandler(ChildWorkDone);
    this.Controls.Add(c);
    }

    private void ChildWorkDone(object sender, EventArgs e)
    {
    this.Controls.Remove(c);
    }
    }


    Whenever I try something like this, I get an HttpException ("A control
    cannot modify its parents' control collections"). True, the child itself
    fired the event, but it's not modifying the collection - the parent is.

    What is the preferred method to accomplish what I'm trying to do? I suppose
    I could always set a boolean or something during the event handler, then
    remove the control at a later stage in the pipeline (like PreRender) but that
    seems "hackish" to me. I have this same thing happening in another app but
    it works fine...

    Thanks for any suggestions.
     
    =?Utf-8?B?SmVmZiBFdmFucw==?=, Nov 19, 2004
    #1
    1. Advertising

  2. I could swear that I've done this before and it worked ... eh, don't
    bother with a flag, just have an ArrayList called something like
    ToRemove. When the event fires, add the sender to the ToRemove array.
    Then, in PreRender, just iterate through the collection and remove
    anything that is in there.

    Hope that helps,
    Joel Martinez
    http://www.onetug.org - Orlando .NET User Group
    http://www.codecube.net - blog

    "Jeff Evans" <> wrote in message news:<>...
    > Hi,
    >
    > I was wondering if it's possible to remove a Control c from a Controls
    > collection in the parent's event handler for an event fired by c.
    >
    > For instance let's say we have
    >
    > public class ChildControl : Control
    > {
    > public event EventHandler WorkDone(object sender, EventArgs e);
    > }
    >
    > public class ParentControl : Control
    > {
    > protected override void OnLoad(EventArgs e)
    > {
    > ChildControl c = new ChildControl();
    > c.WorkDone += new EventHandler(ChildWorkDone);
    > this.Controls.Add(c);
    > }
    >
    > private void ChildWorkDone(object sender, EventArgs e)
    > {
    > this.Controls.Remove(c);
    > }
    > }
    >
    >
    > Whenever I try something like this, I get an HttpException ("A control
    > cannot modify its parents' control collections"). True, the child itself
    > fired the event, but it's not modifying the collection - the parent is.
    >
    > What is the preferred method to accomplish what I'm trying to do? I suppose
    > I could always set a boolean or something during the event handler, then
    > remove the control at a later stage in the pipeline (like PreRender) but that
    > seems "hackish" to me. I have this same thing happening in another app but
    > it works fine...
    >
    > Thanks for any suggestions.
     
    Joel Martinez, Nov 19, 2004
    #2
    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. Purvi T
    Replies:
    0
    Views:
    858
    Purvi T
    Oct 19, 2004
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,134
    David Schwartz
    Dec 9, 2003
  3. caldera

    Placeholder child of child control event problem.

    caldera, May 27, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    216
  4. Tillman

    Make parent control event call child control event

    Tillman, May 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    306
    Tillman
    May 21, 2004
  5. Replies:
    3
    Views:
    999
Loading...

Share This Page