ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Dynamically add and remove UserControl using C# in ASP.Net<p>Here is a very Simple sample in which I have created a Page and UserControl.</p> <p>UserControl contains the TextBoxes and the dynamically added UserControls are maintained using Session.</p> <p><strong><span style="text-decoration: underline;">Page </span></strong></p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeFile=&#34;Default.aspx.cs&#34; Inherits=&#34;_Default&#34; %&gt; &lt;%@ Register Src = &#34;~/UC_TextBoxes.ascx&#34; TagName = &#34;TextBoxes&#34; TagPrefix = &#34;uc&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;asp:Button Text=&#34;AddDigger&#34; runat=&#34;server&#34; OnClick = &#34;AddDigger&#34; /&gt; &lt;asp:Button Text=&#34;RemoveDigger&#34; runat=&#34;server&#34; OnClick = &#34;RemoveDigger&#34; /&gt; &lt;hr /&gt; &lt;asp:Panel ID = &#34;Panel1&#34; runat=&#34;server&#34;&gt; &lt;/asp:Panel&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><span style="text-decoration: underline;">Code</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack) { List&lt;UC_TextBoxes&gt; textBoxes = GetSessionControls(); foreach (UC_TextBoxes textBox in textBoxes) { Panel1.Controls.Add(textBox); } } } protected void AddDigger(object sender, EventArgs e) { UC_TextBoxes ucDigger = (UC_TextBoxes)LoadControl(&#34;~/UC_TextBoxes.ascx&#34;); ; ucDigger.ID = Guid.NewGuid().ToString(); List&lt;UC_TextBoxes&gt; textBoxes = GetSessionControls(); textBoxes.Add(ucDigger); Panel1.Controls.Add(ucDigger); textBoxes.Add(ucDigger); Session[&#34;UserControls&#34;] = textBoxes; } private List&lt;UC_TextBoxes&gt; GetSessionControls() { List&lt;UC_TextBoxes&gt; textBoxes; if (Session[&#34;UserControls&#34;] == null) { textBoxes = new List&lt;UC_TextBoxes&gt;(); } else { textBoxes = (List&lt;UC_TextBoxes&gt;)Session[&#34;UserControls&#34;]; } return textBoxes; }</pre> <p><span style="text-decoration: underline;"><strong>UserControl</strong></span></p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div id=&#34;addOperators&#34; class=&#34;form-group&#34; runat=&#34;server&#34;&gt; &lt;table class=&#34;table&#34;&gt; &lt;tr&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtName1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Operator Name&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtEmpNo1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Operator Emp No&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtST1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Operator ST&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtOT1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Operator OT&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtShift1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Shift&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtTravelTime1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Travel Time&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtTravelRate1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Travel Rate&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID=&#34;txtPerDiem1&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; placeholder=&#34;Per Diem&#34; Width=&#34;90px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/396948/Dynamically-add-and-remove-UserControl-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/396948/Dynamically-add-and-remove-UserControl-using-C-in-ASPNet/Tue, 14 Nov 2017 12:30:22 GMT