Events from a UserControl within a PlaceHolder

Discussion in 'ASP .Net Building Controls' started by Matt Weber, Dec 8, 2004.

  1. Matt Weber

    Matt Weber Guest

    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?
    Matt Weber, Dec 8, 2004
    #1
    1. Advertising

  2. Matt Weber

    Matt Weber Guest

    > 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
    Matt Weber, Dec 14, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben de Vette

    Placeholder and UserControl

    Ben de Vette, Jul 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    8,635
    Ben de Vette
    Jul 12, 2004
  2. Anders K. Jacobsen [DK]

    Problem access usercontrol added to placeholder

    Anders K. Jacobsen [DK], Mar 8, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    509
    Anders K. Jacobsen [DK]
    Apr 1, 2005
  3. protic
    Replies:
    0
    Views:
    350
    protic
    Oct 15, 2008
  4. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    331
  5. Replies:
    2
    Views:
    532
Loading...

Share This Page