Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. User Controls on a PlaceHolder

User Controls on a PlaceHolder

Scheduled Pinned Locked Moved ASP.NET
helpwinformssysadmindata-structuresdebugging
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hhrafn
    wrote on last edited by
    #1

    Hi, I'm trying to make the following scenaryo work... any ideas? :confused: In my main page I put a PlaceHolder like so: <asp:PlaceHolder ID="plhMain" Runat="server"> The code on the main page is like this: test wee = new test(); plhMain.Controls.Add(wee); "test" is a UserControl with the following control: <asp:Label id="Label1" runat="server">Label</asp:Label> It's code is like so: this.Label1.Text="asdf"; When I run this I get the following error: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 17: private void Page_Load(object sender, System.EventArgs e) Line 18: { Line 19: this.Label1.Text="asdf"; Line 20: // Put user code to initialize the page here Line 21: } Any help on this would be greatly appreciated :-D

    M 1 Reply Last reply
    0
    • H hhrafn

      Hi, I'm trying to make the following scenaryo work... any ideas? :confused: In my main page I put a PlaceHolder like so: <asp:PlaceHolder ID="plhMain" Runat="server"> The code on the main page is like this: test wee = new test(); plhMain.Controls.Add(wee); "test" is a UserControl with the following control: <asp:Label id="Label1" runat="server">Label</asp:Label> It's code is like so: this.Label1.Text="asdf"; When I run this I get the following error: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 17: private void Page_Load(object sender, System.EventArgs e) Line 18: { Line 19: this.Label1.Text="asdf"; Line 20: // Put user code to initialize the page here Line 21: } Any help on this would be greatly appreciated :-D

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, To load a web user control, you'd better use the LoadControl method instead of creating a new object with the new keyword, the sample code is something like this:

      test wee = (test) this.LoadControl("test.ascx");
      plhMain.Controls.Add(wee);

      For more information, see Creating Instances of User Controls Programmatically[^]

      H 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, To load a web user control, you'd better use the LoadControl method instead of creating a new object with the new keyword, the sample code is something like this:

        test wee = (test) this.LoadControl("test.ascx");
        plhMain.Controls.Add(wee);

        For more information, see Creating Instances of User Controls Programmatically[^]

        H Offline
        H Offline
        hhrafn
        wrote on last edited by
        #3

        hanks alot, worked like a charm! :-D:-D:-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups