ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Add child controls to User Control in ASP.Net<p>Hi <a class="username" rel="WhatWhat">WhatWhat</a>,</p> <p>can you please explain your requirement in more detail and also do the share the complete code.</p>http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/Thu, 03 Nov 2016 01:55:25 GMTAdd child controls to User Control in ASP.Net<p>Hi!</p> <p>&nbsp;</p> <p>This is my ascx file (popup.ascx):</p> <pre class="brush: html">&lt;%@ Control Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeBehind=&#34;Popup.ascx.cs&#34; Inherits=&#34;LCTools.App.Web.CMS_2017.Secure.Control.Popup&#34; %&gt; &lt;%@ Register assembly=&#34;AjaxControlToolkit&#34; namespace=&#34;AjaxControlToolkit&#34; tagprefix=&#34;ajaxToolkit&#34; %&gt; &lt;asp:LinkButton ID=&#34;LinkButtonPopupDummy&#34; runat=&#34;server&#34; /&gt; &lt;ajaxToolkit:ModalPopupExtender ID=&#34;LinkButtonPopupDummy_ModalPopupExtender&#34; runat=&#34;server&#34; BehaviorID=&#34;LinkButtonPopupDummy_ModalPopupExtender&#34; TargetControlID=&#34;LinkButtonPopupDummy&#34; PopupControlID=&#34;PanelMain&#34; CancelControlID=&#34;LinkButtonClose&#34; BackgroundCssClass=&#34;popup_background&#34;&gt; &lt;/ajaxToolkit:ModalPopupExtender&gt; &lt;asp:Panel ID=&#34;PanelMain&#34; runat=&#34;server&#34;&gt; &lt;div class=&#34;popup&#34;&gt; &lt;div&gt; &lt;div class=&#34;popup_header&#34;&gt; &lt;table style=&#34;width:100%;&#34;&gt; &lt;tr&gt; &lt;td style=&#34;text-align:center;&#34;&gt; &lt;asp:UpdatePanel ID=&#34;UpdatePanelHeader&#34; runat=&#34;server&#34; UpdateMode=&#34;Conditional&#34;&gt; &lt;ContentTemplate&gt; &lt;asp:Literal ID=&#34;LiteralHeader&#34; runat=&#34;server&#34; /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/td&gt; &lt;td style=&#34;width:8px;text-align:right;&#34;&gt; &lt;asp:LinkButton ID=&#34;LinkButtonClose&#34; runat=&#34;server&#34; Text=&#34;[X]&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&#34;popup_content&#34;&gt; &lt;asp:UpdatePanel ID=&#34;UpdatePanelContent&#34; runat=&#34;server&#34;&gt; &lt;ContentTemplate&gt; &lt;asp:PlaceHolder ID=&#34;PlaceHolderContent&#34; runat=&#34;server&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; &lt;/div&gt; &lt;/asp:Panel&gt;</pre> <p>&nbsp;</p> <p>This is the code behind:</p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.ComponentModel; using System.Security.Permissions; namespace LCTools.App.Web.CMS_2017.Secure.Control { [ AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), DefaultProperty(&#34;Inner&#34;), ParseChildren(true, &#34;Inner&#34;), ToolboxData( &#34;&lt;{0}:Popup runat=\&#34;server\&#34;&gt; &lt;/{0}:Popup&gt;&#34;) ] public partial class Popup : Lib.baseControl { private List&lt;System.Web.UI.Control&gt; _content; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack &amp;&amp; !this.Loaded) { foreach(System.Web.UI.Control c in this.Inner) { PlaceHolderContent.Controls.Add(c); } } } public void Show(CommandButton.Types type) { string header = string.Empty; switch (type) { case CommandButton.Types.CreateItem: header = &#34;Legg til ny vare&#34;; break; case CommandButton.Types.CreateGroup: header = &#34;Legg til ny gruppe&#34;; break; } LiteralHeader.Text = header; UpdatePanelHeader.Update(); LinkButtonPopupDummy_ModalPopupExtender.Show(); } private bool Loaded { get { return ViewState[&#34;PopupLoaded&#34;] != null ? (bool)ViewState[&#34;PopupLoaded&#34;] : false; } set { ViewState[&#34;PopupLoaded&#34;] = value; } } [Category(&#34;Behavior&#34;), DefaultValue(&#34;&#34;), Description(&#34;Inner controls&#34;), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerDefaultProperty)] public List&lt;System.Web.UI.Control&gt; Inner { get { if (_content == null) { _content = new List&lt;System.Web.UI.Control&gt;(); } return _content; } } } }</pre> <p>&nbsp;</p> <p>What Im trying to do is to avoid writing the same code everytime I need a popup dialog. I use ModalPopupExtender from Ajax Control Toolkit.</p> <p>And this is how I want to use the user control:</p> <pre class="brush: html">&lt;uc1:Popup runat=&#34;server&#34; ID=&#34;PopupExample&#34;&gt;&lt;asp:Image ID=&#34;ImageNoContent&#34; runat=&#34;server&#34; ImageURL=&#34;some_picture.jpg&#34; /&gt;&lt;/uc1:Popup&gt;</pre> <p>When I need the dialog I call Show, and the idea is to add all the controls between the start and end tag to a ContentPlaceHolder (ID: PlaceHolderContent).</p> <p>Is this possible, or am I way off??</p> <p>&nbsp;</p> <p>Thanks for replying!</p> <p>&nbsp;</p>http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/Thu, 03 Nov 2016 06:09:14 GMTAdd child controls to User Control in ASP.Net<p>Hi WhatWhat,</p> <p>You cant add the controls in usercontrol's panel in aspx page directly you need to dynamically add the controls in panel of usercontrol on the button or linkbutton click on which you are showing the modalpopupextender.</p>http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/Thu, 03 Nov 2016 07:55:44 GMTAdd child controls to User Control in ASP.Net<p>Hurray! it worked!</p> <p>I can make the content as user controls, and pass in Type and load it dynamically.</p> <p>&nbsp;</p> <p>Thank you very much! :)</p>http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/Thu, 03 Nov 2016 08:26:26 GMTAdd child controls to User Control in ASP.Net<p>Will do! here goes :)</p> <p>&nbsp;</p> <p><strong>Popup.ascx:</strong></p> <pre class="brush: html">&lt;%@ Control Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeBehind=&#34;Popup.ascx.cs&#34; Inherits=&#34;LCTools.App.Web.CMS_2017.Secure.Control.Popup&#34; %&gt; &lt;%@ Register assembly=&#34;AjaxControlToolkit&#34; namespace=&#34;AjaxControlToolkit&#34; tagprefix=&#34;ajaxToolkit&#34; %&gt; &lt;asp:LinkButton ID=&#34;LinkButtonPopupDummy&#34; runat=&#34;server&#34; /&gt; &lt;ajaxToolkit:ModalPopupExtender ID=&#34;LinkButtonPopupDummy_ModalPopupExtender&#34; runat=&#34;server&#34; BehaviorID=&#34;LinkButtonPopupDummy_ModalPopupExtender&#34; TargetControlID=&#34;LinkButtonPopupDummy&#34; PopupControlID=&#34;PanelMain&#34; CancelControlID=&#34;LinkButtonClose&#34; BackgroundCssClass=&#34;popup_background&#34;&gt; &lt;/ajaxToolkit:ModalPopupExtender&gt; &lt;asp:Panel ID=&#34;PanelMain&#34; runat=&#34;server&#34;&gt; &lt;div class=&#34;popup&#34;&gt; &lt;div&gt; &lt;div class=&#34;popup_header&#34;&gt; &lt;table style=&#34;width:100%;&#34;&gt; &lt;tr&gt; &lt;td style=&#34;text-align:center;&#34;&gt; &lt;asp:UpdatePanel ID=&#34;UpdatePanelHeader&#34; runat=&#34;server&#34; UpdateMode=&#34;Conditional&#34;&gt; &lt;ContentTemplate&gt; &lt;asp:Literal ID=&#34;LiteralHeader&#34; runat=&#34;server&#34; /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/td&gt; &lt;td style=&#34;width:8px;text-align:right;&#34;&gt; &lt;asp:LinkButton ID=&#34;LinkButtonClose&#34; runat=&#34;server&#34; Text=&#34;[X]&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&#34;popup_content&#34;&gt; &lt;asp:UpdatePanel ID=&#34;UpdatePanelContent&#34; runat=&#34;server&#34;&gt; &lt;ContentTemplate&gt; &lt;asp:Panel ID=&#34;PanelContent&#34; runat=&#34;server&#34;&gt;&lt;/asp:Panel&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; &lt;/div&gt; &lt;/asp:Panel&gt;</pre> <p>&nbsp;</p> <p><strong>Code behind:</strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.ComponentModel; namespace LCTools.App.Web.CMS_2017.Secure.Control { public partial class Popup : Lib.baseControl { protected void Page_Load(object sender, EventArgs e) { } public void Show(CommandButton.Types type) { string header = string.Empty; switch (type) { case CommandButton.Types.CreateItem: header = &#34;Legg til ny vare&#34;; PanelContent.Controls.Add(Page.LoadControl(&#34;/Secure/Control/PopupModule/CreateItem.ascx&#34;)); break; case CommandButton.Types.CreateGroup: header = &#34;Legg til ny gruppe&#34;; break; } LiteralHeader.Text = header; UpdatePanelHeader.Update(); LinkButtonPopupDummy_ModalPopupExtender.Show(); } } }</pre> <p>&nbsp;</p> <p><strong>CreateItem.ascx:</strong></p> <pre class="brush: html">&lt;%@ Control Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeBehind=&#34;CreateItem.ascx.cs&#34; Inherits=&#34;LCTools.App.Web.CMS_2017.Secure.Control.PopupModule.Shop.CreateItem&#34; %&gt; &lt;asp:Literal ID=&#34;LiteralDemo&#34; runat=&#34;server&#34; Text=&#34;Just for show&#34; /&gt;</pre> <p>&nbsp;</p> <p><strong>In use:</strong></p> <pre class="brush: html">&lt;uc1:Popup runat=&#34;server&#34; ID=&#34;PopupDemo&#34; /&gt;</pre> <p><strong>Code behind:&nbsp;</strong>(Button click event)</p> <pre class="brush: csharp">PopupDemo.Show(Secure.Control.CommandButton.Types.CreateItem);</pre> <p>&nbsp;</p> <p>thanks again</p>http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/http://www.aspforums.net/Threads/789494/Add-child-controls-to-User-Control-in-ASPNet/Thu, 03 Nov 2016 09:18:00 GMT