locked
loading custom server control dynamically RRS feed

  • Question

  • User-357021789 posted

    hello,

    I have a custom server control which I can load statically in aspx page. how do I do load it dynamically in code behind? I have tried the Page_Init event but the controls defined in the aspx page and their initiation code seems to have been loaded by the time we hit Page_Init.

       protected void Page_Init(object sender, EventArgs e)
    {
    Scon.ID = "dynamSCon";
    Scon.value = "MyValue";
    AddedControl(SCon,0);
    }
    
    
    I have a feelign that this is due to implemenation of the control itself
    
    
    
    

    Can someone please help. 

    Monday, October 3, 2011 3:22 PM

Answers

  • User3866881 posted

    Hello Ridgeback:)

    Page_Init() is an event that will be raised at first. So maybe it hasn't been initialized. So you cannot dynamically add controls onto the page itself.

    Maybe you can do this in the Page_Load:

    protected void Page_Load(object sender, EventArgs e)
        {      
            Controls.Add(Your new control here);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 10:13 PM

All replies

  • User-357021789 posted

    so I found that if I add my dynamic control to a placeholder defined in my aspx. My server control will get added to the asp.net live cycle. 

    Why is is that I can't just do a page.addcontrol?

    Monday, October 3, 2011 4:45 PM
  • User3866881 posted

    Hello Ridgeback:)

    Page_Init() is an event that will be raised at first. So maybe it hasn't been initialized. So you cannot dynamically add controls onto the page itself.

    Maybe you can do this in the Page_Load:

    protected void Page_Load(object sender, EventArgs e)
        {      
            Controls.Add(Your new control here);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 10:13 PM