Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start with, here is a working demo, which you can reference throughout my answer: <a href="http://jsfiddle.net/EuyB8/" rel="nofollow noreferrer">http://jsfiddle.net/EuyB8/</a>.</p> <p>Obviously the demo is in plain HTML rather than ASP.NET, but I will try to address the differences.</p> <p>To start, since you will want to create some of the controls that you'll be cloning using .NET controls, my typical approach is to create a hidden template, which can then be used to create new copies. As such, I would have a row like the following:</p> <pre><code>&lt;tr id="TemplateRow"&gt; &lt;td&gt; &lt;asp:DropDownList ID="ddl_BoxName" runat="server"&gt; &lt;asp:ListItem Value="attr1" Selected="True"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="attr2"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="attr3"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID="ddl_BoxComparision" runat="server"&gt; &lt;asp:ListItem Value="=" Selected="true"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="&gt;"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="&lt;"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="Like"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="!="&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID="btn_boxval" runat="server" &gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:CheckBox ID="chk_DeleteBoxRow" runat="server" /&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>I then add a CSS rule like so:</p> <pre><code>#TemplateRow { display:none; } </code></pre> <p>Now we have a row that we can use as a template for adding new rows, and the actual markup for the template can still be generated by .NET. It is important to note that when using this approach with a table, the template row needs to be inside the table to which you'll be appending the rows, in order to ensure that the cells are the appropriate width.</p> <p>The last bit of markup we'll need to do is to give the table an ID, so that we can manipulate the rows in our script. I chose "BoxTable".</p> <p>Now, let's construct our script. Because you are using .NET, it's important to remember that for any tag with the <code>runat="server"</code> attribute, the ID attribute that is generated is not the same as the one you assign in the control's ID field. An easy workaround for that is to do something like the following:</p> <pre><code>var myClientID = '&lt;%= myServerID.ClientID() %&gt;'; </code></pre> <p>The server then outputs the client ID into a string for easy use in your script.</p> <p>With that in mind, here's our script:</p> <pre><code>var btn_AddAttr = '&lt;%= btn_AddAttr.ClientID() %&gt;'; $(function() { //attach the a function to the click event of the "Add Box Attribute button that will add a new row $('#' + btn_AddAttr).click(function() { //clone the template row, and all events attached to the row and everything in it var $newRow = $('#TemplateRow').clone(true); //strip the IDs from everything to avoid DOM issues $newRow.find('*').andSelf().removeAttr('id'); //add the cloned row to the table immediately before the last row $('#BoxTable tr:last').before($newRow); //to prevent the default behavior of submitting the form return false; }); //attach a remove row function to all current and future instances of the "remove row" check box $('#DeleteBoxRow').click(function() { //find the closest parent row and remove it $(this).closest('tr').remove(); }); //finally, add an initial row by simulating the "Add Box Attribute" click $('#' + btn_AddAttr).click(); }); </code></pre> <p>I think the comments are pretty thorough, but a brief explanation of a few points is warranted:</p> <p>First, when we clone the template row, we are passing a boolean to the clone function. This says that in addition to the DOM elements we are cloning, we should also clone the event handlers attached to those elements, and attach them to the new clones. This is important in order for the "Delete Row" checkbox to work.</p> <p>Second, when we clone the template row, we are also cloning all the attributes of all the elements, including the IDs. We don't want this because it violates XHTML compliance and my cause problems. Thus, we strip all the cloned elements of their IDs.</p> <p>Lastly, because you are using a submit button, it's important to remember to return false in the button's click event, to avoid having it submit the form and cause a postback.</p> <p>Hopefully this answers your question. One final thing to bear in mind is that since you are creating all these rows with jQuery, the server will not have objects ready to receive the values of the inputs. If you need the server to have access to that information, you'll have to figure out so method of sending it that information.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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