click events no longer work on dynamically loaded web user control

G

Guest

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?
 
J

Jesse Houwing

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
....
}
 
G

Guest

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top