dynamically create multiple instances of usercontrol in codebehind

P

Padam

I created a user conrolt in C# and want to create multiple instance (one by
one) of usercontrol onclick of button. if i hard code in .aspx page like

<%@ Register Src="~/UserControls/AddPhaseMilestone.ascx"
TagName="AddPhaseMilestone" TagPrefix="uc1" %>

<uc1:AddPhaseMilestone ID="AddPhaseMilestone1" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone2" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone3" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone4" EnableViewState="true"
runat="server" />

it works fine. I have to do in codebehind using click event to button. Pls
do suggest me how to do that. I tried below mention code but it is not
solving my problem.



Control oControl = Page.LoadControl("~/UserControls/AddPhaseMilestone.ascx");

//oControl.ID = "fajkla";

//PlaceHolder1.Controls.Add(oControl);

//div.Controls.Add(oControl);

Page.Controls.Add(oControl);

pls suggest some alternative solutions also, if possible
 
R

Riki

Padam said:
I created a user conrolt in C# and want to create multiple instance
(one by one) of usercontrol onclick of button. if i hard code in
.aspx page like

<%@ Register Src="~/UserControls/AddPhaseMilestone.ascx"
TagName="AddPhaseMilestone" TagPrefix="uc1" %>

<uc1:AddPhaseMilestone ID="AddPhaseMilestone1" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone2" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone3" EnableViewState="true"
runat="server" />

<uc1:AddPhaseMilestone ID="AddPhaseMilestone4" EnableViewState="true"
runat="server" />

it works fine. I have to do in codebehind using click event to
button. Pls do suggest me how to do that. I tried below mention code
but it is not solving my problem.



Control oControl =
Page.LoadControl("~/UserControls/AddPhaseMilestone.ascx");

//oControl.ID = "fajkla";

//PlaceHolder1.Controls.Add(oControl);

//div.Controls.Add(oControl);

Page.Controls.Add(oControl);

pls suggest some alternative solutions also, if possible

This code is all right.
Why is it not solving your problem?
What is your problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top