dynamically create multiple instances of usercontrol in codebehind

Discussion in 'ASP .Net Building Controls' started by Padam, Aug 28, 2007.

  1. Padam

    Padam Guest

    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
     
    Padam, Aug 28, 2007
    #1
    1. Advertisements

  2. Padam

    Riki Guest

    This code is all right.
    Why is it not solving your problem?
    What is your problem?
     
    Riki, Aug 28, 2007
    #2
    1. Advertisements

  3. Padam

    Padam Guest

    It is not working when i create instances of user control in code behind.
    padam
     
    Padam, Aug 28, 2007
    #3
  4. Padam

    Jens Wefer Guest

    you must also load dynamically usercontrols after a postback.
     
    Jens Wefer, Aug 28, 2007
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.