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