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. Advertising

  2. Padam

    Riki Guest

    Padam wrote:
    > 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?

    --

    Riki
    Riki, Aug 28, 2007
    #2
    1. Advertising

  3. Padam

    Padam Guest

    Re: dynamically create multiple instances of usercontrol in codebe

    It is not working when i create instances of user control in code behind.
    padam

    "Riki" wrote:

    > Padam wrote:
    > > 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?
    >
    > --
    >
    > Riki
    >
    >
    >
    Padam, Aug 28, 2007
    #3
  4. Padam

    Jens Wefer Guest

    Re: dynamically create multiple instances of usercontrol in codebe

    you must also load dynamically usercontrols after a postback.
    Padam schrieb:
    > It is not working when i create instances of user control in code behind.
    > padam
    >
    > "Riki" wrote:
    >
    >> Padam wrote:
    >>> 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?
    >>
    >> --
    >>
    >> Riki
    >>
    >>
    >>
    Jens Wefer, Aug 28, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGFkYW0=?=
    Replies:
    1
    Views:
    1,411
    Petar Atanasov
    Aug 28, 2007
  2. DaveF

    multiple datagrids dynamically from the codebehind

    DaveF, May 3, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    116
    Michael Tkachev
    May 5, 2004
  3. J055
    Replies:
    5
    Views:
    994
    Steven Cheng[MSFT]
    Dec 25, 2006
  4. santoshp

    Dynamically adding multiple instances of same usercontrol

    santoshp, May 30, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    207
    santoshp
    May 30, 2007
  5. Padam
    Replies:
    0
    Views:
    235
    Padam
    Aug 28, 2007
Loading...

Share This Page