All controls are null within usercontrol

后端 未结 4 2226
醉酒成梦
醉酒成梦 2021-02-18 13:27

I have a UserControl which uses a UserControl, among other controls.

In the ascx file I have the following code:

<%@ Register TagPrefix=         


        
4条回答
  •  [愿得一人]
    2021-02-18 14:22

    I had this problem when I was adding a user control in the code behind the wrong way. You have to use the Page.LoadControl method to initialize the control you can't just use new.

            //WRONG
            UserControls.BingoCardPage bcp = new UserControls.BingoCardPage();
            form1.Controls.Add(bcp);
            //RIGHT
            UserControls.BingoCardPage bcp = (UserControls.BingoCardPage)Page.LoadControl("~/UserControls/BingoCardPage.ascx");
            form1.Controls.Add(bcp);
    

提交回复
热议问题