Search code examples
c#asp.netwebusercontrol

Is it possible to load a WebUserControl.ascx to a panel in default page like we have in Winforms


I am having a default page namely default.aspx in this i will have panel. And i will have 2 other forms namely Webusercontrol1.ascx and Webusercontrol2.ascx i will design the page with some controls now is it possible to load this page in to the panel which was on default page like as we did in WINFORMS.


Solution

  • Web user controls (.ascx) can definitely be loaded into a panel.

    Web forms (.aspx) cannot.

    Are you trying to dynamically load your user controls into a panel? If so, you can do that too, but you have to do it on every postback.

    To add a control to your panel from the code behind, simply create it, and then add it to the Controls collection of the panel. Just remember, this has to be repeated on every subsequent postback.

    WebUserControl uc = new WebUserControl();
    panel1.Controls.Add(uc);
    

    Just be sure to register your user control in your aspx file

    <%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc" TagName="WebControl"  %>