Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add controls dynamically in .net?
    text
    copied!<p>I want to add controls dynamically</p> <p>Code:</p> <p>.aspx</p> <pre><code>&lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:Label ID="lbl1" Text="Personal Information" Font-Size="Large" ForeColor="White" runat="server" Width="854px" BackColor="SteelBlue" style="margin-top: 0px" Height="60px"&gt; &lt;/asp:Label&gt; &lt;div id="div1" runat="server"&gt; &lt;asp:Panel ID="panelPersonal" runat="server"&gt; &lt;div id="divreg" runat="server"&gt; &lt;table id="tbl" runat="server"&gt; &lt;tr&gt; &lt;td class="style8"&gt;&amp;nbsp; Visa Number:&lt;/td&gt; &lt;td class="style20"&gt;&amp;nbsp;&lt;asp:TextBox ID="txtUser" Width="160px" runat="server"/&gt;&lt;/td&gt; &lt;td class="style22"&gt;&amp;nbsp; Country Name:&lt;/td&gt; &lt;td class="style23"&gt;&amp;nbsp;&lt;asp:DropDownList ID="dropCountry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="style22"&gt;&amp;nbsp; Type of Visa:&lt;/td&gt; &lt;td class="style23"&gt;&amp;nbsp;&lt;asp:DropDownList ID="dropVisa" Width="165px" runat="server"&gt; &lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;td class="style22"&gt;&amp;nbsp; Type of Entry:&lt;/td&gt; &lt;td class="style23"&gt;&amp;nbsp;&lt;asp:DropDownList ID="dropEntry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="style8"&gt;&amp;nbsp; Expiry Date&lt;/td&gt; &lt;td class="style20"&gt; &amp;nbsp;&lt;%--&lt;BDP:BasicDatePicker ID="BasicDatePicker4" runat="server" onselectionchanged="BasicDatePicker2_SelectionChanged" AutoPostBack="True" /&gt;--%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/asp:Panel&gt; &lt;asp:Button ID="addnewtext" runat="server" Text="Add" onclick="addnewtext_Click" width="76px" /&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p></p> <p>Here I am using User control</p> <p>.ascx</p> <pre><code> &lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %&gt; &lt;div id="divreg" runat="server"&gt; &lt;table id="tbl" runat="server"&gt; &lt;tr&gt; &lt;td&gt; Visa Number:&lt;/td&gt; &lt;td&gt;&lt;asp:TextBox ID="txtUser" Width="160px" runat="server"/&gt;&lt;/td&gt; &lt;td&gt; Country Name:&lt;/td&gt; &lt;td&gt;&lt;asp:DropDownList ID="dropCountry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Type of Visa:&lt;/td&gt; &lt;td&gt;&lt;asp:DropDownList ID="dropVisa" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;td&gt; Type of Entry:&lt;/td&gt; &lt;td&gt;&lt;asp:DropDownList ID="dropEntry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Expiry Date&lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>.aspx.cs</p> <p>Here is the problem If I click add button one row is added successfully but when I close and open again previously added rows also coming.</p> <p>I think the problem is static int i = 0;</p> <p>Any other way is there?</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } static int i = 0; protected void addnewtext_Click(object sender, EventArgs e) { i++; for (int j = 0; j &lt;= i; j++) { AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx"); PlaceHolder1.Controls.Add(ac); PlaceHolder1.Controls.Add(new LiteralControl("&lt;BR&gt;")); } } </code></pre> <p>This is what I am getting</p> <p><img src="https://i.stack.imgur.com/bslOl.png" alt="enter image description here"></p> <p>This is what I want, Like below image I want to do</p> <p><img src="https://i.stack.imgur.com/r0blV.png" alt="enter image description here"></p> <p>Any ideas? Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload