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

3.7k Views Asked by At

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.

3

There are 3 best solutions below

1
On BEST ANSWER

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"  %>
1
On

I think this is the one i required

UserControl usr1 = (UserControl)LoadControl("WebUserControl.ascx");
2
On

In ascx:

<%@ Register src="RC01.ascx" tagname="RC01" tagprefix="uc1" %>
<asp:Panel ID="Panel1" runat="server"> </asp:Panel>

In ascx.cs

protected void Page_Load(object sender, EventArgs e)
{
    RC01 uc1 = (RC01)LoadControl("RC01.ascx");
    Panel1.Controls.Add(uc1);
}