How to add user control on run time in ASP.NET?

后端 未结 4 1704
情话喂你
情话喂你 2021-01-18 23:55

How do I add a user control on page load dynamically in ASP.NET?

I have a div with ID=\"contentData\" and few controls

  • one.ascx
  • t
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 00:38

    In your aspx

    In Page_Load

        UserControl uc = (UserControl)Page.LoadControl("test.ascx");
        div1.Controls.Add(uc);
    

    All you need to do is make your div server bound by adding runat="server", and in the codebehind use Page.LoadControl to go out and fetch your usercontrol and then add it to your div using the div's Controls.Add

提交回复
热议问题