Events from a UserControl within a PlaceHolder

M

Matt Weber

Brief background: I have a page that loads n UserControls into a PlaceHolder
control, where n corresponds to a number of inputs by the user. Each
UserControl consists of a couple of labels, a TextBox, an ImageButton, and a
DataGrid.

I'm trying to fire an event from within the UserControl to notify the page
that it sits on that the UserControl needs to be deleted. The event is fired
when the user clicks on the ImageButton that resides within my UserControl.
This works just fine. However, after the page is loaded again sans the
deleted control, none of the ImageButtons retain their Click event handler.
If the ImageButton is clicked I get a postback, and then the event handlers
get hooked up again. So essentially, after deleting the first control, the
ImageButton has to be clicked twice to delete any subsequent control.
Furthermore, if the controls being deleted are the last ones within the
PlaceHolder, everything works fine. The problem only occurs when deleting a
control that is not last within the PlaceHolder.

Within the page I am sessioning information to reload the previously loaded
UserControls into the PlaceHolder on page load. Thus, when the delete event
is fired, all of the controls on the page are reloaded into the PlaceHolder
(including the control I'm going to delete). The control to delete is then
removed from the PlaceHolder and my session data, and then the PlaceHolder is
reloaded with the remaining controls.

I've noticed that, because I'm loading the controls on postback, then
clearing the PlaceHolder control and reloading all but the deleted control,
the control prefixes for the UserControls within the PlaceHolder do not start
at zero. This makes sense, because the UserControls are being loaded twice,
but it seems like it might be part of the reason my events are being
disconnected.

Thanks for reading.

Any thoughts?
 
M

Matt Weber

Long story, but what I think you should do is check on Page.IsPostback
before you recreate all controls.
Don't recreate, just state

if (!Page.IsPostback)
{
// Create all
}
else
{
// do nothing
}

When the control should be deleted you could call something like
(inside the UC)

void {Event}(args)
this.Parent.DeleteMeFromPlaceHolder(this.Name);

Is that an answer to your question, if not, try reposting your
question just a bit shorter, no offense.

Thanks for your response. I've kind of worked around the issue by marking
my user controls as deleted within the session variable and then
showing/hiding them as necessary. This works for me, because once a control
has been deleted, there's no need to show it again for the lifetime of the
visit to the page.

The problem, I think, was just me doing something incorrectly when deleting
that caused the wrong controls to be deleted under certain circumstances.

Matt
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top