No events are fired in child control

C

Casper Hornstrup

I have developed a custom web control that may contain any child controls.
Now I have a problem that even though I register an event handler on a child
control,
the event handler does not fire when it should. Do I need to handle event
registrations
of child controls specially?

Casper
 
A

Alessandro Zifiglio

hi casper,
Your going to have to override your onBubbleEvent method for your control
and take action here or bubble it up futher to the container(the page).
providing a common event handler for all events fired. To differeniate btw
controls and who is posting back your end users will have to supply a name
to the buttons commandName property which is exposed by controls that
postback and then let them use this to handle handle events in one common
handler.
 
J

Jeffrey Tan[MSFT]

Hi Casper,

Thank you for posting in the community!

Based on my understanding, you are building a custom control, in which you
created several child controls. Althrough you have added event handler for
your child control, this event handle does not take effect.

===================================================
Where do you create your child controls? Do you create your child control
through override Control.CreateChildControls?

Acutally, in Composite Control, if you want to hook the child controls'
event, you must implement the System.Web.UI.INamingContainer interface. It
will provide you the unique names in the hierarchical tree of controls. If
you do not implement this interface, asp.net model will not associate the
event handler well.

For example, this code will work well:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
public class EventBubbler : System.Web.UI.WebControls.WebControl,
INamingContainer
{
protected override void CreateChildControls()
{
Button button1 = new Button();
button1.Text = "Click";
button1.Click+=new EventHandler(button1_Click);
Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
this.Page.Response.Write("Child button event fires");
}
}
}

But this will not fire your event:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
public class EventBubbler : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
Button button1 = new Button();
button1.Text = "Click";
button1.Click+=new EventHandler(button1_Click);
Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
this.Page.Response.Write("Child button event fires");
}
}
}

For more information, please refer to "Developing a Composite Control" at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpcondevelopingcompositecontrols.asp

================================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
C

Casper Hornstrup

Hi Jeffrey.

I implemented INamingContainer in the control and events now work.

Thanks,
Casper
 
A

Alessandro Zifiglio

hi Jeffery, thanks for pointing this out. Somehow I had always missed that
part on the docs where it says that implementing the INamingContainer
interface is important also when you need to route events to its child
controls. I was always under the impression that when inheriting from
webControls this interface was useless the reason being that I started
facing naming problems for child controls when trying to pass an explicit ID
whereas to the generic uniqueID supplied by asp.net

However now i see that supplying "clientID" & "_name" or me.ID & "_name" to
a child controls ID is wrong. This has just fixed a bug a bug for me in my
control.
Thanks again ;)

"Jeffrey Tan[MSFT]" said:
Hi Casper,

Thank you for posting in the community!

Based on my understanding, you are building a custom control, in which you
created several child controls. Althrough you have added event handler for
your child control, this event handle does not take effect.

===================================================
Where do you create your child controls? Do you create your child control
through override Control.CreateChildControls?

Acutally, in Composite Control, if you want to hook the child controls'
event, you must implement the System.Web.UI.INamingContainer interface. It
will provide you the unique names in the hierarchical tree of controls. If
you do not implement this interface, asp.net model will not associate the
event handler well.

For example, this code will work well:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
public class EventBubbler : System.Web.UI.WebControls.WebControl,
INamingContainer
{
protected override void CreateChildControls()
{
Button button1 = new Button();
button1.Text = "Click";
button1.Click+=new EventHandler(button1_Click);
Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
this.Page.Response.Write("Child button event fires");
}
}
}

But this will not fire your event:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace CustomControls
{
public class EventBubbler : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
Button button1 = new Button();
button1.Text = "Click";
button1.Click+=new EventHandler(button1_Click);
Controls.Add(button1);
}

private void button1_Click(object sender, EventArgs e)
{
this.Page.Response.Write("Child button event fires");
}
}
}

For more information, please refer to "Developing a Composite Control" at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpcondevelopingcompositecontrols.asp

================================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Casper,

Thanks for your feedback.

I am glad it works.

If you have any further concern, please feel free to tell me, I will work
with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Alessandro,

Yes, I also through INamingContainer is useless in the past, but not now.
:)
You are welcome.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top