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.Dynamically add and remove User Control to PlaceHolder using C# in ASP.Net<p>Hi yorke,</p> <p>I have created a sample which full fill your requirement</p> <p><span style="text-decoration: underline;">UserControl</span></p> <pre class="brush: html">&lt;table&gt; &lt;tr&gt; &lt;td&gt; Name: &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Country: &lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID=&#34;ddlCountry&#34; runat=&#34;server&#34;&gt; &lt;asp:ListItem Text=&#34;-Select-&#34; /&gt; &lt;asp:ListItem Text=&#34;India&#34; /&gt; &lt;asp:ListItem Text=&#34;USA&#34; /&gt; &lt;asp:ListItem Text=&#34;Canada&#34; /&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Education: &lt;/td&gt; &lt;td&gt; &lt;asp:CheckBoxList ID=&#34;cblEducation&#34; runat=&#34;server&#34;&gt; &lt;asp:ListItem Text=&#34;BTech&#34; /&gt; &lt;asp:ListItem Text=&#34;MCA&#34; /&gt; &lt;asp:ListItem Text=&#34;MTech&#34; /&gt; &lt;/asp:CheckBoxList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;asp:Button ID=&#34;btnRemove&#34; runat=&#34;server&#34; OnClick=&#34;btnRemove_Click&#34; Text=&#34;Remove&#34; /&gt; &lt;hr /&gt;</pre> <p><span style="text-decoration: underline;">UserControl.ascx.cs</span></p> <pre class="brush: csharp">//Declare the event that we want to raise (we&#39;ll handle this in the parent page) public event EventHandler RemoveUserControl; protected internal void btnRemove_Click(object sender, System.EventArgs e) { //Raise this event so the parent page can handle it RemoveUserControl(sender, e); }</pre> <p><span style="text-decoration: underline;">CS.aspx</span></p> <pre class="brush: html">&lt;%@ Register Src=&#34;~/UserControl.ascx&#34; TagName=&#34;UserControl&#34; TagPrefix=&#34;uc1&#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;div&gt; &lt;asp:ScriptManager ID=&#34;sm1&#34; runat=&#34;server&#34; /&gt; &lt;asp:UpdatePanel ID=&#34;up1&#34; runat=&#34;server&#34;&gt; &lt;ContentTemplate&gt; &lt;div class=&#34;demo&#34;&gt; &lt;asp:PlaceHolder ID=&#34;ph1&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnAdd&#34; runat=&#34;server&#34; Text=&#34;Add&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;demo&#34;&gt; &lt;asp:Literal ID=&#34;ltlValues&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnDisplayValues&#34; runat=&#34;server&#34; Text=&#34;Display Values&#34; OnClick=&#34;btnDisplayValues_Click&#34; /&gt; &lt;/div&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;asp:Literal ID=&#34;ltlCount&#34; runat=&#34;server&#34; Text=&#34;0&#34; Visible=&#34;false&#34; /&gt; &lt;asp:Literal ID=&#34;ltlRemoved&#34; runat=&#34;server&#34; Visible=&#34;false&#34; /&gt; &lt;/div&gt; &lt;style type=&#34;text/css&#34;&gt; div.demo { width: 300px; float: left; padding: 20px; margin: 10px; border: solid green 1px; } &lt;/style&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;">CS.aspx.cs</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, System.EventArgs e) { AddAndRemoveDynamicControls(); } private void AddAndRemoveDynamicControls() { Control c = GetPostBackControl(Page); if ((c != null)) { if (c.ID.ToString() == &#34;btnAdd&#34;) { ltlCount.Text = (Convert.ToInt16(ltlCount.Text) + 1).ToString(); } } ph1.Controls.Clear(); int ControlID = 0; for (int i = 0; i &lt;= (Convert.ToInt16(ltlCount.Text) - 1); i++) { UserControl DynamicUserControl = (UserControl)LoadControl(&#34;UserControl.ascx&#34;); while (InDeletedList(&#34;uc&#34; + ControlID) == true) { ControlID += 1; } DynamicUserControl.ID = &#34;uc&#34; + ControlID; DynamicUserControl.RemoveUserControl += this.HandleRemoveUserControl; ph1.Controls.Add(DynamicUserControl); ControlID += 1; } } private bool InDeletedList(string ControlID) { string[] DeletedList = ltlRemoved.Text.Split(&#39;|&#39;); for (int i = 0; i &lt;= DeletedList.GetLength(0) - 1; i++) { if (ControlID.ToLower() == DeletedList[i].ToLower()) { return true; } } return false; } public void HandleRemoveUserControl(object sender, EventArgs e) { Button remove = (sender as Button); UserControl DynamicUserControl = (UserControl)remove.Parent; ph1.Controls.Remove((UserControl)remove.Parent); ltlRemoved.Text += DynamicUserControl.ID + &#34;|&#34;; ltlCount.Text = (Convert.ToInt16(ltlCount.Text) - 1).ToString(); } protected void btnAdd_Click(object sender, System.EventArgs e) { //Handled in page load } protected void btnDisplayValues_Click(object sender, System.EventArgs e) { ltlValues.Text = &#34;&#34;; foreach (Control c in ph1.Controls) { if (c.GetType().Name.ToLower() == &#34;usercontrol_ascx&#34;) { UserControl uc = (UserControl)c; TextBox tbx1 = uc.FindControl(&#34;txtName&#34;) as TextBox; DropDownList ddl1 = uc.FindControl(&#34;ddlCountry&#34;) as DropDownList; CheckBoxList cbx1 = uc.FindControl(&#34;cblEducation&#34;) as CheckBoxList; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(&#34;Name: &#34; + tbx1.Text + &#34;&lt;br /&gt;&#34;); sb.Append(&#34;Country: &#34; + ddl1.SelectedValue + &#34;&lt;br /&gt;&#34;); sb.AppendLine(&#34;Education: &#34;); foreach (ListItem li in cbx1.Items) { if (li.Selected == true) { sb.Append(li.Value + &#34;&lt;br /&gt;&#34;); } } sb.Append(&#34;&lt;hr /&gt;&#34;); ltlValues.Text += sb.ToString(); } } } //Find the control that caused the postback. public Control GetPostBackControl(Page page) { Control control = null; string ctrlname = page.Request.Params.Get(&#34;__EVENTTARGET&#34;); if ((ctrlname != null) &amp; ctrlname != string.Empty) { control = page.FindControl(ctrlname); } else { foreach (string ctl in page.Request.Form) { Control c = page.FindControl(ctl); if (c is System.Web.UI.WebControls.Button) { control = c; break; } } } return control; }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><img src="https://i.imgur.com/bVWmcmW.gif" alt="" width="472" height="331" /></p>http://www.aspforums.net/Threads/350354/Dynamically-add-and-remove-User-Control-to-PlaceHolder-using-C-in-ASPNet/http://www.aspforums.net/Threads/350354/Dynamically-add-and-remove-User-Control-to-PlaceHolder-using-C-in-ASPNet/Tue, 27 Sep 2016 03:35:31 GMT