WebUserControl and Dynamic Control Creation

Posted on 2008-11-06
Last Modified: 2010-10-05
I've a simple web user control, inside it, I've some dropdownlists that I populate and fill them in Page_Init event of page.
When I drag and drop my control on page, it works well and as expected.

But when I try to dynamically add controls to a page, (for example in Page_PreInit event of the page, by coding:
        WebUserControl w = new WebUserControl();

"Object reference not set to an instance of an object." error occurs (for this.DropDownList1.Items.Add("1");)

I think I should read more about Page Life Cycle, but any direct help, would be highly appreciated.
Question by:Mortaza Doulaty
    1 Comment
    LVL 5

    Accepted Solution

    Add a 'PlaceHolder' and try like this.

    Control FeaturedProductUserControl = LoadControl("WebUserControl1.ascx");

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now