Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > No events are fired in child control

Reply
Thread Tools

No events are fired in child control

 
 
Casper Hornstrup
Guest
Posts: n/a
 
      01-29-2004
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


 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      01-29-2004
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.


"Casper Hornstrup" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      01-30-2004

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/de...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.

 
Reply With Quote
 
Casper Hornstrup
Guest
Posts: n/a
 
      01-30-2004
Hi Jeffrey.

I implemented INamingContainer in the control and events now work.

Thanks,
Casper


""Jeffrey Tan[MSFT]"" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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/de...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.
>



 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      01-30-2004
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]"" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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/de...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.
>



 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      01-30-2004

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.

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      01-30-2004

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded <divs> with events: How to prevent the parent div's eventfrom being fired when the embedded div's event is fired? Num GG Javascript 2 11-17-2008 08:56 PM
DataGrid control events like Sort, page, Item not getting fired... Gunjan Garg ASP .Net 0 07-12-2005 05:02 PM
How do I capture events fired in child user controls? Justin ASP .Net Web Controls 4 01-27-2004 09:51 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Catching events in web form fired by user control Nick Lewis ASP .Net 1 08-01-2003 12:01 PM



Advertisments