Answered by:
Dynamically Load UserControl on Tab Click

Question
-
User-725520357 posted
Hi,
i want to Load UserControl Dynamically when Ajax Tab is Clicked...
i have code for that...it is working fine when tab is clicked once...if i click the same tab again datas disappears...
Please help me with this...
my code is:
Default ------------------ <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Menu.ascx.cs" Inherits="UserControl_Menu" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <%@ Reference Control="~/UserControl/Emp1.ascx" %> <%@ Reference Control="~/UserControl/Emp2.ascx" %> <%@ Reference Control="~/UserControl/Emp3.ascx" %> <script language="javascript" type="text/javascript"> function ActiveTabChanged(sender, e) { // use the client side active tab changed // event to trigger a callback thereby // triggering the server side event. __doPostBack('<%= Tab1.ClientID %>', sender.get_activeTab().get_headerText()); } </script> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanelmain" runat="server"> <ContentTemplate> <cc1:TabContainer ID="Tab1" runat="server" OnClientActiveTabChanged="ActiveTabChanged" OnActiveTabChanged="AddUserContol"> <cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1"> <ContentTemplate> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </cc1:TabPanel> <cc1:TabPanel runat="server" ID="TabPanel2" HeaderText="TabPanel2"> <ContentTemplate> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </cc1:TabPanel> <cc1:TabPanel runat="server" ID="TabPanel3" HeaderText="TabPanel3"> <ContentTemplate> <asp:UpdatePanel ID="UpdatePanel3" runat="server"> <ContentTemplate> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> </cc1:TabPanel> </cc1:TabContainer> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Tab1" EventName="ActiveTabChanged" /> </Triggers> </asp:UpdatePanel> <asp:PlaceHolder ID="phModule" runat="server"></asp:PlaceHolder> ---------------------------- Default.ascx.cs ----------------------------- using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using AjaxControlToolkit; public partial class UserControl_Menu : System.Web.UI.UserControl { protected ASP.usercontrol_emp1_ascx control1; protected ASP.usercontrol_emp2_ascx control2; protected ASP.usercontrol_emp3_ascx control3; protected void Page_Load(object sender, EventArgs e) { // ScriptManager1.RegisterAsyncPostBackControl(Tab1); if (!IsPostBack) { control1 = (ASP.usercontrol_emp1_ascx)LoadControl("~/UserControl/Emp1.ascx"); UpdatePanel1.ContentTemplateContainer.Controls.Add(control1); } } protected void AddUserContol(object sender, EventArgs e) { if (Tab1.ActiveTab.ID == "TabPanel1") { control1 = (ASP.usercontrol_emp1_ascx)LoadControl("~/UserControl/Emp1.ascx"); UpdatePanel1.ContentTemplateContainer.Controls.Add(control1); } if (Tab1.ActiveTab.ID == "TabPanel2") { control2 = (ASP.usercontrol_emp2_ascx)LoadControl("~/UserControl/Emp2.ascx"); UpdatePanel2.ContentTemplateContainer.Controls.Add(control2); } if (Tab1.ActiveTab.ID == "TabPanel3") { control3 = (ASP.usercontrol_emp3_ascx)LoadControl("~/UserControl/Emp3.ascx"); UpdatePanel3.ContentTemplateContainer.Controls.Add(control3); } } }
Sunday, July 3, 2011 1:55 AM
Answers
-
User555306248 posted
Hi, Please refer this
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 3, 2011 11:34 PM
All replies
-
User-1637433209 posted
Check out this post: http://lionsden.co.il/codeden/?p=154. It explains how to maintain the a dynamicaly loaded user control and also has an example to download. In short, the serverSide does not know the user's selection in Page_Init. You need to run LoadUserControl() from the event fired by the tab.
Sunday, July 3, 2011 2:10 PM -
User555306248 posted
Hi, Please refer this
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 3, 2011 11:34 PM