|
|
|
start date: Thu, 16 Aug 2007 21:10:01 -0700,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
Jim
|
|
2
Jesse Houwing am
|
|
3
Jim
|
click events no longer work on dynamically loaded web user control
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?
Date:Thu, 16 Aug 2007 21:10:01 -0700
Author:
|
Re: click events no longer work on dynamically loaded web user control
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
Date:Fri, 17 Aug 2007 13:25:19 +0000 (UTC)
Author:
|
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
>
>
>
Date:Sun, 19 Aug 2007 20:32:07 -0700
Author:
|
|
|