Bubbling Event in Composite controls

N

Nidhee

Hi Everyone,


I am building a composite server control. I have defined an event for
my child control with Custom Event Arguments. I am using the bubbling
method to access the child control's events in the Parent control.
Still, my child control is not raising the event. What am I missing?
Following is the code snippet:

//Child control code

public class ChildClass: System.Web.UI.WebControls.WebControl,
ISerializable, IPostBackEventHandler, INamingContainer
{
public event CustomEventHandler DoubleClick;
protected override void Render(HtmlTextWriter writer){ ... }

#region IPostBackEventHandler Members
public void RaisePostBackEvent(string eventArgument)
{
CustomEventArgs cusArgs = new CustomEventArgs(this.PKID);
if (DoubleClick != null)
{
DoubleClick(this, cusArgs);
RaiseBubbleEvent(this, cusArgs);
}
}

#endregion

#region CustomEventArgs class
//CustomEventArgs class
public class CustomEventArgs: EventArgs
{
public CustomEventArgs(int PKID)
{
this.PKID = PKID;
}

public int PKID;
} //end of class RectangleEventArgs
#endregion
}

//Parent control code
public class ParentClass : System.Web.UI.WebControls.WebControl,
IPostBackEventHandler

public event CustomEventHandler GetCustomProperties;
protected override void CreateChildControls() { ... }

protected override bool OnBubbleEvent(object source, EventArgs args)
{
bool handled = false;
if(GetCustomProperties != null)
if(args is CustomEventArgs)
{
CustomEventArgs e = (CustomEventArgs)args;
GetCustomProperties(this, e);
handled = true;
}
return handled;
}
 
J

John Saunders

Nidhee said:
Hi Everyone,


I am building a composite server control. I have defined an event for
my child control with Custom Event Arguments. I am using the bubbling
method to access the child control's events in the Parent control.
Still, my child control is not raising the event. What am I missing?

Did you try to catch the event directly, without bubbling?
 
N

Nidhee

John Saunders said:
Did you try to catch the event directly, without bubbling?

Thanks for your response.
Yes, I tried to catch the Child's event directly. The event is working
fine when the child is not contained in the parent control. But the
instance of the child control in the parent control does not Raise a
Postback event. Looks like the parent control needs to handle the
event somehow. I am not able to figure out, how.

Nidhee
 
J

John Saunders

Nidhee said:
"John Saunders" <[email protected]> wrote in message

Thanks for your response.
Yes, I tried to catch the Child's event directly. The event is working
fine when the child is not contained in the parent control. But the
instance of the child control in the parent control does not Raise a
Postback event. Looks like the parent control needs to handle the
event somehow. I am not able to figure out, how.

Pardon me, I meant did you try to directly catch the event in the parent?

Right now, you are not receiving the bubbled event. This could either be
because the child is not raising the event, or because the child is raising
the event but bubbling isn't working. If you are able to directly catch the
child event, you will know that the problem is with bubbling.

When I say catch the event directly, I mean something like the following:

protected override void CreateChildControls()
{
...
Button btn = new Button();
btn.Text = "Button text";
btn.Click += new EventHandler(btn_Click);

Controls.Add(btn);
...
}

private void btn_Click(object sender, EventArgs e)
{
Page.Trace.Write("Button click caught");
}
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top