Abstraction

Many new developers encounter a situation where there is a need to add dynamic control on the client side. This article will help them to understand how to add dynamic control on the client side using jQuery. Here I have described it using a step-by-step approach.

Initial chamber

Step 1

Open Visual Studio and create an empty website, provide a suitable name such as AddDynamicTextBox.

Step 2

In Solution Explorer you will get your empty website. Then add web forms.

For Web Form

AddDynamicTextBox (your empty website). Right-click and select Add New Item, then Web Form. Name it AddDynamicTextBox.aspx.

Design chamber

Step 3

Open AddDynamicTextBox.aspx file and write some code for the design of the application.

First add the jQuery plugin to your head section. Here I have used jquery-1.10.2.js plugin for demonstration purposes.

How to add in your page

  1. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
Write some script for add or remove as in the following:
  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $('#add').click(function () {
  4. var table = $(this).closest('table');
  5. console.log(table.find('input:text').length);
  6. if (table.find('input:text').length < 50) {
  7. var x = $(this).closest('tr').nextAll('tr');
  8. $.each(x, function (i, val) {
  9. val.remove();
  10. });
  11. table.append('<tr><td style="width:200px;" align="right">First Name <td> <input type="text" id="current Name" value="" /> </td><td style="width:200px;" align="right">Last Name <td> <input type="text" id="current Name" value="" /> </td></tr>');
  12. $.each(x, function (i, val) {
  13. table.append(val);
  14. });
  15. }
  16. });
  17. $('#del').click(function () {
  18. var table = $(this).closest('table');
  19. if (table.find('input:text').length > 1) {
  20. table.find('input:text').last().closest('tr').remove();
  21. }
  22. });
  23. });
  24. </script>
Add HTML on your page and your page will look like the following:
  1. <div>
  2. <table border="0" cellspacing="2">
  3. <tr>
  4. <td style="width: 200px;" align="right">Name
  5. <td>
  6. <input type="text" id="Job Name" value="" />
  7. </td>
  8. </td>
  9. </tr>
  10. <tr>
  11. <td align="right">Versions</td>
  12. <td>
  13. <select id="version" style="width: 350px;">
  14. <option value="">SELECT</option>
  15. </select>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td align="right">Test Scripts</td>
  20. <td>
  21. <select id="testscripts" style="width: 350px;"></select>
  22. </td>
  23. </tr>
  24. <tr>
  25. <td align="right">datas</td>
  26. <td>
  27. <input type="button" id="add" value="Add" />
  28. <input type="button" id="del" value="Del" />
  29. </td>
  30. </tr>
  31. <tr>
  32. <td style="height: 3px" colspan="2"></td>
  33. </tr>
  34. </table>
  35. </div>
Now your code looks as in the following:

design

Now your page looks as in the following.

AddDynamicTextBox.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddDynamicTextBox.aspx.cs" Inherits="AddDynamicTextBox" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>C# corner article</title>
  6. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  7. <script type="text/javascript">
  8. $(document).ready(function () {
  9. $('#add').click(function () {
  10. var table = $(this).closest('table');
  11. console.log(table.find('input:text').length);
  12. if (table.find('input:text').length < 50) {
  13. var x = $(this).closest('tr').nextAll('tr');
  14. $.each(x, function (i, val) {
  15. val.remove();
  16. });
  17. table.append('<tr><td style="width:200px;" align="right">First Name <td> <input type="text" id="current Name" value="" /> </td><td style="width:200px;" align="right">Last Name <td> <input type="text" id="current Name" value="" /> </td></tr>');
  18. $.each(x, function (i, val) {
  19. table.append(val);
  20. });
  21. }
  22. });
  23. $('#del').click(function () {
  24. var table = $(this).closest('table');
  25. if (table.find('input:text').length > 1) {
  26. table.find('input:text').last().closest('tr').remove();
  27. }
  28. });
  29. });
  30. </script>
  31. </head>
  32. <body>
  33. <form id="form1" runat="server">
  34. <div>
  35. <table border="0" cellspacing="2">
  36. <tr>
  37. <td style="width: 200px;" align="right">Name
  38. <td>
  39. <input type="text" id="Job Name" value="" />
  40. </td>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td align="right">Versions</td>
  45. <td>
  46. <select id="version" style="width: 350px;">
  47. <option value="">SELECT</option>
  48. </select>
  49. </td>
  50. </tr>
  51. <tr>
  52. <td align="right">Test Scripts</td>
  53. <td>
  54. <select id="testscripts" style="width: 350px;"></select>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td align="right">datas</td>
  59. <td>
  60. <input type="button" id="add" value="Add" />
  61. <input type="button" id="del" value="Del" />
  62. </td>
  63. </tr>
  64. <tr>
  65. <td style="height: 3px" colspan="2"></td>
  66. </tr>
  67. </table>
  68. </div>
  69. </form>
  70. </body>
  71. </html>
In the code behind page, there is no need to write something here because I've provided everything from the front end.

AddDynamicTextBox.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class AddDynamicTextBox : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. }
Output

Here's a screenshot showing the interface before adding the TextBox.

Before creating textbox
Figure 1

Here's a screenshot showing the interface after adding the TextBox.

After creating
Figure 2

add text box
Figure 3

textbox
Figure 4

I hope you liked this. Have a good day. Thank you for reading.