Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

click events no longer work on dynamically loaded web user control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 17 Aug 2007 05:10 GMT
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?
Jesse Houwing - 17 Aug 2007 14:25 GMT
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
[quoted text clipped - 10 lines]
> 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
Jim - 20 Aug 2007 04:32 GMT
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

> Hello jim,
>
[quoted text clipped - 42 lines]
> Jesse Houwing
> jesse.houwing at sogeti.nl

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.