Hello Developers,
I have a project in VS2008, i am having requirement to load control dynamically. So for that i am trying to develop one demo for it but i am not succeed yet.

So i am here...

I have developed one page namely (Default.aspx) and three user-control (UserControl1.ascx, UserControl2.ascx, UserControl3.ascx)

I want to dynamically add and remove controls as per the button clicked event.

I have kept two buttons on every control, for loading any of them. ( For Example, if UserControl1 is loaded then Loadcontrol2 and LoadControl3 Button is there on usercontrol1 for calling the controls.)

My code is as Below:
Default.aspx

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
            
</asp:PlaceHolder>
<input type="hidden" id="hdnControls" runat="server" value="UC_Control1.ascx" />
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{       
       String strControl = ((HtmlInputHidden)FindControl("hdnControls")).Value;
       Control c1 = LoadControl(strControl);
       PlaceHolder1.Controls.AddAt(0, c1);
}
UserControl1.ascx
<asp:Panel ID="Panel1" runat="server">
    Control 1
    <asp:Button ID="btnControl2" runat="server" Text="Call Control 2" 
    onclick="btnControl2_Click" />
    <asp:Button ID="btnControl3" runat="server" Text="Call Control 3" 
    onclick="btnControl3_Click" />
</asp:Panel>

UserControl1.ascx.cs

protected void btnControl2_Click(object sender, EventArgs e)
{
        ((HtmlInputHidden)Parent.FindControl("hdnControls")).Value = "UC_Control2.ascx";
}


  But the problem is every time I have to click twice time on button then and then only the another control get loaded on default.aspx

  Could you guys suggest what i am doing wrong ? If you suggest me Another solution then 

i am also thankful all you in advanced with hopeing, will soon get Reply..

I have tried every thing possible on this earth.. I am calling controls dynamically using button click on the main page.. using loadcontrol and viewstate.. but if i have a button in usercontrol1 which has to replace the usercontrol1 with usercontrol2 in the same placeholder.. i cant do it.. I thing the new planet has to be reinvented

Helo Thanks.. May be i got my answer.. you were doing it perfect but facing problem because not maintaining the viewstate. or session... Will try your method and revert back tomorrow.

Hi man,, Your solution is working perfect. But most of the problem is sorted,.. Even proper postback is happening.. But the main problem perists.. ""replacing usercontrol in placeholder by button click in usercontrol""... But I Can say it is working but mailfunctioning.. If i click the button which is suppose to perform function in the same control. it works on single click but if the button is suppose to replace the usercontrol in the placeholder of the main page then it works on 2 clicks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.