How do I capture events fired in child user controls?

J

Justin

First, let me give you some background on what I am trying to accomplish. I have a page which contains a custom user control, lets call it Navigator, that has some buttons next, back, etc. Navigator also contains a placeholder in which I load other custom user controls when next or back is clicked. These custom user controls contain textboxes and dropdown lists which contain info that I need in Navigator.

The problem is, when as a user I change text in the textboxes of Navigator's placeholder child controls no events are firing and the text is not being saved. Now, I am new to the whole asp.net world and I am thinking that I am missing some very fundamental knowledge that might help me capture the events of the child controls. Does this problem sound familiar to anyone? If so, what might I be doing wrong? Please help

Thanks

Justin
 
E

Eran Amitai

Justin, I know there are user controls and custom controls. What are custom
user controls?

Eran

Justin said:
First, let me give you some background on what I am trying to accomplish.
I have a page which contains a custom user control, lets call it Navigator,
that has some buttons next, back, etc. Navigator also contains a
placeholder in which I load other custom user controls when next or back is
clicked. These custom user controls contain textboxes and dropdown lists
which contain info that I need in Navigator.
The problem is, when as a user I change text in the textboxes of
Navigator's placeholder child controls no events are firing and the text is
not being saved. Now, I am new to the whole asp.net world and I am thinking
that I am missing some very fundamental knowledge that might help me capture
the events of the child controls. Does this problem sound familiar to
anyone? If so, what might I be doing wrong? Please help.
 
A

Alessandro Zifiglio

hi justin, can you provide some code as to how your going about adding your
controls to the placeholder, when you are setting the properties of your
dynamically added controls. You need to set properties and the events after
the controls have been added to the controls collections. And make sure this
code is run also after postback.

Futhermore can you elaborate as to what you mean by no events are fire --in
shorts the steps you have taken to hook up event handlers etc
Each user control has a code behind file associated with it and this is the
right place to code. Include any event-handling methods here and also
declare public instances of the ASP.NET server controls that you plan to use
within your usercontrol here. Keep in mind, however, that user controls
encapsulate their own events and send the event information through the
containing page to be processed. Do not include user control event handlers
in the containing page, write them in the codebehind file that generates the
user control.

a straight forward answer to your post heading "How do i capture events
fired in child user controls is to use event bubbling. That is you bubble up
all events in your child user controls up to their containers and take
action there or bubble them up further etc. Not sure if this is what you
want but makes sense to me.
To bubble up events you need to override the onBubbleEvent function for you
usercontrol and then capture these in your parent container for that
usercontrol.


Justin said:
First, let me give you some background on what I am trying to accomplish.
I have a page which contains a custom user control, lets call it Navigator,
that has some buttons next, back, etc. Navigator also contains a
placeholder in which I load other custom user controls when next or back is
clicked. These custom user controls contain textboxes and dropdown lists
which contain info that I need in Navigator.
The problem is, when as a user I change text in the textboxes of
Navigator's placeholder child controls no events are firing and the text is
not being saved. Now, I am new to the whole asp.net world and I am thinking
that I am missing some very fundamental knowledge that might help me capture
the events of the child controls. Does this problem sound familiar to
anyone? If so, what might I be doing wrong? Please help.
 
A

Alessandro Zifiglio

try overriding the onbubbleEvent function of your container and check to see
who is postingback, if its the onNext button try and retrieve your text.
http://www.aboutfortunate.com/Default.aspx

As you will note from that code in the link, they are overriding the
onBubbleEvent of the container, in your case this is the same place where
you are trying to retrieve the values. That is where you loaded the
control(The container for your usercontrol)

I think you are trying to retrieve your values before your usercontrol has
been restored. Surely you already know that when dynamically loading
controls the viewstate is not restored until the control is reloaded again.
If you can trap the event in your container and try retrieving the value
when this event fires, that should work for you. Otherwise try retrieving
the value right after the code where you are reloading your dynamic controls
fire, or any methods that fire afterwards.

The code you posted is still very little and I dont know on what method you
are loading your controls, and if that code is run after a postback etc nor
what the flow of your program is, but surely you should be on the right
track now ;)


Justin said:
OK Alessandro,

Here is the code you requested:

// Load wizard placeholder with next user control
wizardTabPlaceHolder = GetPlaceHolder(wizardTab); // This just finds the placeholder in a tab control
wizardTabPlaceHolder.Controls.Remove(currentUserControl); // I remove the last visited control
WizardUserControl nextControl =
(WizardUserControl)Page.LoadControl(controlFileName); // Now I load the control
wizardTabPlaceHolder.Controls.Add(nextControl); // THen add it to the placeholder.

So, according to you there are some extra things that I need to do. What properties do I need to set?

At this point the only controls within this user control being loaded are
labels and textboxes. So as a user, when a user control is loaded I type in
text, then I click next. Now I want to be able to retreive the text in the
code behind of this control before I load the new control. The problem is
when I try to get the text the textboxes claim to be empty strings. This is
what I mean by no events firing. I guess its more of a timing thing, the
events are probably firing but I do not know when to catch them so that I
can get the text.
The code above is called in my OnNext() method. I try to retreive the
text before the call to the above code.
As far as bubbling the events up, I have not done that yet. I guess
because I do not know how to do it dynamically.
 
J

Justin

About the difference between a custom control and a user control, I guess I am confused and don't really know the difference. Maybe you can clear this up for me.

Thanks,

Justin
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top