Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue in adding/removing table rows dynamically in ASP .NET using jQuery
    primarykey
    data
    text
    <p>I have a user control which allows user to enter multiple email recipients. On page load, I have one table row containing name and email address textbox(with-out Remove button) </p> <p>All other table rows are being added using jQuery and each row thus added has a Remove button which allow the user to delete a particular row. Below is my code:</p> <pre><code>&lt;div&gt; &lt;table cellspacing="5" runat="server" id="tblEmailRecipients" clientidmode="Static"&gt; &lt;tr&gt; &lt;td colspan="4"&gt;Custom Email Recipients&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3"&gt; &lt;input type="button" value="Add More" onclick="AddNewTableRow();" id="btnAdd" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr style="font-weight:bold"&gt; &lt;td&gt;Name&lt;/td&gt; &lt;td&gt;Email Address&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;asp:TextBox runat="server" ID="txtName" &gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;td&gt;&lt;asp:TextBox runat="server" ID="txtEmailAddress" &gt;&lt;/asp:TextBox&gt; &lt;asp:RegularExpressionValidator ID="regexValidatorEmailAddress" runat="server" ErrorMessage="Invalid Email Address" ControlToValidate="txtEmailAddress" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"&gt;&lt;/asp:RegularExpressionValidator&gt; &lt;/td&gt; &lt;td&gt; &lt;%--&lt;input type="button" value="Remove" onclick="DeleteTableRow(this);" id="btnRemove" name="btnRemove" /&gt;--%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" style="height: 26px" /&gt; </code></pre> <p>jQuery:</p> <pre><code>function DeleteTableRow(ctrl) { //delete control table row $($(ctrl).closest("tr")).remove(); } function AddNewTableRow() { //Get Row clone and add as last row //Also, created row clone but with different control ids var i = $("#tblEmailRecipients tr").length; var newRow = $("#tblEmailRecipients tr:last").clone().find("input").each( function () { $(this).attr({ 'id': function (_, id) { return id + i }, 'name': function (_, name) { return name + i }, }); }).end(); $("#tblEmailRecipients tr:last").after(newRow); $("#tblEmailRecipients tr:last input:text").val(""); //Add 'Remove' button in the last column - this needs to be done only for second row, all rows after 2nd row will auto. contain Remove button as they are cloned from prev. row var lastCell = $("#tblEmailRecipients tr:last td:last"); var lastCellContents = jQuery.trim($(lastCell).html()); if (lastCellContents == '') { var btnRemoveName = "btnRemove" + i; var btnRemove = "&lt;input type='button' value='Remove' onclick='DeleteTableRow(this);' id='" + btnRemoveName + "' name='" + btnRemoveName + "' /&gt;"; $(btnRemove).appendTo($(lastCell)); } //Move focus to newly added row Textbox $("#tblEmailRecipients tr:last input:first").focus(); } </code></pre> <p>The issue I am facing is while handling btnSave_Click,<br> Even if I add 2 or more recipients(table rows), In the code behind - I see only first table row containing email recipient. </p> <p>I wonder why I am not able to access other table rows that were added at run-time... they also go missing on UI after postback?</p> <p>Could you please guide.</p> <p>Thank you! </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.
 

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