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 / Building Controls / September 2003

Tip: Looking for answers? Try searching our database.

Events not firing on dynamically added UserControls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Gerdes - 23 Sep 2003 20:13 GMT
I am trying to dynamically add a user control to a Custom Control that
inherits a Panel. The control renders fine on the web form.  The
problem is that none of the user control's events are firing (button
clicks, etc.) on the server.  The page is posting back, but none of
the user control's event delegates are being hit.

I am trying to load the User Control in the PreRender of the Custom
Control (I have tried putting this in different places with no
success).

Any help would be hugely appreciated.

Thanks,

James

Here's the code for the web form on which I'm trying to instantiate
the custom control:

     public class WebForm3 : System.Web.UI.Page
     {
           private WebCustomControl1 cc;

           private void Page_Load(object sender, System.EventArgs e)
           {
                 //instantiate the custom panel
                 cc = new
WebCustomControl1("~/WebUserControl1.ascx");
                 //add the panel to this page's form
                 this.Controls[1].Controls.Add(cc);
           }

           #region Web Form Designer generated code
           override protected void OnInit(EventArgs e)
           {
                 //
                 // CODEGEN: This call is required by the ASP.NET Web
Form Designer.
                 //
                 InitializeComponent();
                 base.OnInit(e);
           }

           /// <summary>
           /// Required method for Designer support - do not modify
           /// the contents of this method with the code editor.
           /// </summary>
           private void InitializeComponent()
           {    
                 this.Load += new
System.EventHandler(this.Page_Load);
           }
           #endregion
     }

And here's the entire code for my custom control:

     public class WebCustomControl1 : Panel
     {
           public WebCustomControl1(string url)
           {
                 m_PlaceHolder = new PlaceHolder();
                 m_Url = url;
           }

           protected override void OnPreRender(EventArgs e)
           {
                this.m_PlaceHolder.Controls.Add(Page.LoadControl(m_Url));
                this.Controls.Add(m_PlaceHolder);
                 base.OnPreRender (e);
           }
     }
Victor Garcia Aprea [MVP] - 25 Sep 2003 04:41 GMT
Hi James,

>>>> I am trying to load the User Control in the PreRender of the Custom
>>>> Control (I have tried putting this in different places with no
>>>> success).
That's the problem. PreRender is too late as event firing logic has already
passed by and your usercontrol didn't existed at that time. You need to add
them earlier, ie: Init.

This[1] may give you some tips.

[1] http://weblogs.asp.net/vga/posts/23498.aspx

Signature

Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.

> I am trying to dynamically add a user control to a Custom Control that
> inherits a Panel. The control renders fine on the web form.  The
[quoted text clipped - 68 lines]
>             }
>       }

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



©2009 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.