click events no longer work on dynamically loaded web user control

Discussion in 'ASP .Net' started by =?Utf-8?B?Smlt?=, Aug 17, 2007.

  1. I have an application that was built using asp.net 1.1. The application
    interviews the user with a parent form that loads a series of web user
    controls (.ascx files)dynamically with page.loadcontrol. It needs to be
    dynamic because the next user control loaded is based on the answer to the
    previous question. The application has worked flawlessly for many years. I
    recently upgraded the application to .net 2.0 and found that the button click
    events on the web user control are no longer firing. After closer
    examination I found that when I set a breakpoint on the parent form the
    postback is happening there instead of the click event handler on the .ascx
    web user control.

    Can anyone give me a suggestion on how to get the click event to go to the
    user control instead of the parent?
     
    =?Utf-8?B?Smlt?=, Aug 17, 2007
    #1
    1. Advertising

  2. Hello jim,

    > I have an application that was built using asp.net 1.1. The
    > application interviews the user with a parent form that loads a series
    > of web user controls (.ascx files)dynamically with page.loadcontrol.
    > It needs to be dynamic because the next user control loaded is based
    > on the answer to the previous question. The application has worked
    > flawlessly for many years. I recently upgraded the application to
    > .net 2.0 and found that the button click events on the web user
    > control are no longer firing. After closer examination I found that
    > when I set a breakpoint on the parent form the postback is happening
    > there instead of the click event handler on the .ascx web user
    > control.
    >
    > Can anyone give me a suggestion on how to get the click event to go to
    > the user control instead of the parent?
    >


    Can you post the code that loads the controls? My guess is that it has to
    do with the order in which things are loaded and or given their ID.

    These are the basic rules for dynamic usercontrol loading:

    OnInit/Page_load !Postback, load the usercontrols
    OnInit/Page_load Postback, load the usercontrols that were loaded before
    the postback occured. Give them exactly the same ID as before. Always give
    them ID, otherwise events won't fire.

    PreRender Postback, if you need to load different controls, remove the old
    ones. Load the new controls

    When adding controls always do the following sequence: load, id, add, rest:

    MyUserControl myControl = Page.LoadControl("name") as MyUserControl;
    if (myControl != null)
    {
    myControl.ID = "anID";
    Page.Controls.Add(myControl); // Or someContainer.Controls.Add(..)
    // set other properties now.
    ....
    // register event handlers now
    ....
    }

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
     
    Jesse Houwing, Aug 17, 2007
    #2
    1. Advertising

  3. Re: click events no longer work on dynamically loaded web user con

    Hi Jesse - Thanks for replying and I very much appreciate your help. With
    your advice I was able to fix my problem. I had to change the way I was
    handling the postback.

    This is the code with the changes annotated:

    protected void LoadWizardUserControl()
    {
    //Get handle on the wizard user control within this page
    Microsoft.Web.UI.WebControls.PageView pv =
    (Microsoft.Web.UI.WebControls.PageView)this.MyMultiPage.Controls[0];

    if (!this.IsPostBack)
    {
    //Load new content page within multipage
    AddsCOA.cWizardUserControl myuc;

    myuc =
    this.curWorkOrderRec.currentPageUserControl =
    (AddsCOA.cWizardUserControl)LoadControl(
    this.curWorkOrderRec.currentPageURL);
    myuc.ID = "WizardContents";

    pv.Controls.Clear();
    pv.Controls.Add(myuc);

    myuc.LoadData();
    }
    else
    {
    // oiginal code worked in .net1.1 does not work in .net2.0
    //pv.Controls.Add(
    // this.curWorkOrderRec.currentPageUserControl );


    //new code below works in .net 2.0
    AddsCOA.cWizardUserControl myuc;

    myuc =
    this.curWorkOrderRec.currentPageUserControl =
    (AddsCOA.cWizardUserControl)LoadControl(
    this.curWorkOrderRec.currentPageURL);
    myuc.ID = "WizardContents";

    pv.Controls.Clear();
    pv.Controls.Add(myuc);
    }
    }

    Jim Cristofono


    "Jesse Houwing" wrote:

    > Hello jim,
    >
    > > I have an application that was built using asp.net 1.1. The
    > > application interviews the user with a parent form that loads a series
    > > of web user controls (.ascx files)dynamically with page.loadcontrol.
    > > It needs to be dynamic because the next user control loaded is based
    > > on the answer to the previous question. The application has worked
    > > flawlessly for many years. I recently upgraded the application to
    > > .net 2.0 and found that the button click events on the web user
    > > control are no longer firing. After closer examination I found that
    > > when I set a breakpoint on the parent form the postback is happening
    > > there instead of the click event handler on the .ascx web user
    > > control.
    > >
    > > Can anyone give me a suggestion on how to get the click event to go to
    > > the user control instead of the parent?
    > >

    >
    > Can you post the code that loads the controls? My guess is that it has to
    > do with the order in which things are loaded and or given their ID.
    >
    > These are the basic rules for dynamic usercontrol loading:
    >
    > OnInit/Page_load !Postback, load the usercontrols
    > OnInit/Page_load Postback, load the usercontrols that were loaded before
    > the postback occured. Give them exactly the same ID as before. Always give
    > them ID, otherwise events won't fire.
    >
    > PreRender Postback, if you need to load different controls, remove the old
    > ones. Load the new controls
    >
    > When adding controls always do the following sequence: load, id, add, rest:
    >
    > MyUserControl myControl = Page.LoadControl("name") as MyUserControl;
    > if (myControl != null)
    > {
    > myControl.ID = "anID";
    > Page.Controls.Add(myControl); // Or someContainer.Controls.Add(..)
    > // set other properties now.
    > ....
    > // register event handlers now
    > ....
    > }
    >
    > --
    > Jesse Houwing
    > jesse.houwing at sogeti.nl
    >
    >
    >
     
    =?Utf-8?B?Smlt?=, Aug 20, 2007
    #3
    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. Danny Bloodworth

    Events in dynamically loaded user controls

    Danny Bloodworth, Nov 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    557
  2. Earl Teigrob
    Replies:
    1
    Views:
    7,715
    nfedin
    Feb 10, 2004
  3. Thanh Nguyen
    Replies:
    0
    Views:
    446
    Thanh Nguyen
    Apr 1, 2004
  4. Raed Sawalha
    Replies:
    1
    Views:
    615
    Karl Seguin
    Oct 7, 2004
  5. =?Utf-8?B?SHV6ZWZh?=
    Replies:
    2
    Views:
    351
    =?Utf-8?B?SHV6ZWZh?=
    Oct 8, 2007
Loading...

Share This Page