asp.net – Can not add Custom Control into UpdatePanel – Education Career Blog

My code:

    protected void Button1_Click(object sender, EventArgs e)
    {

        CustomControl myControl = new CustomControl();
        UpdatePanel1.ContentTemplateContainer.Controls.Add(myControl);

        Button btn = new Button();
        UpdatePanel1.ContentTemplateContainer.Controls.Add(btn);
    }

After click new button appears but my control did not. CustomControl if fine(?) since I can add it into .aspx file and it work without any problem.

What did I wrong?

btw. There is no errors or warnings, it just don’t add custom control to html page.

,

I found a solution based on discussion from related topic.

now is:

CustomControl myControl = new CustomControl();

should be:

string controlPath = @"~/Controls/CustomControl.ascx";
CustomControl myControl = (CustomControl)LoadControl(controlPath);

Leave a Comment