Thursday, 11 December 2014

How to Add TextBox Dynamically Using JavaScript in ASP.Net

In this article I am explain , How to create Dynamic textboxs in Asp.net using javascript and get that's values in Code Behind.

Download Here

1-Create Textboxs:
2-Fill Values
 3-Get Values

Javascript:


<script type="text/javascript">
        var count = "1";
        function addRow(in_tbl_name) {
            var tbody = document.getElementById(in_tbl_name).getElementsByTagName("TBODY")[0];
            // create row
            var row = document.createElement("TR");
            // create table cell 2
            var td1 = document.createElement("TD")
            var strHtml2 = "<input type=\"text\" name=\"SName\" size=\"30\" maxlength=\"30\" />";
            td1.innerHTML = strHtml2.replace(/!count!/g, count);
            // create table cell 3
            var td2 = document.createElement("TD")
            var strHtml3 = "<input type=\"text\" name=\"Email\" size=\"30\" maxlength=\"30\" />";
            td2.innerHTML = strHtml3.replace(/!count!/g, count);
            // create table cell 4
            var td3 = document.createElement("TD")
            var strHtml4 = "<input type=\"text\" name=\"Address\" size=\"30\" maxlength=\"30\" />";
            td3.innerHTML = strHtml4.replace(/!count!/g, count);
            // create table cell 5
            var td4 = document.createElement("TD")
            var strHtml5 = "<img src=\"delete.png\" onclick=\"deleteRow(this.parentNode.parentNode.rowIndex)\" style=\"width: 22px; cursor:pointer;\" />";
            td4.innerHTML = strHtml5.replace(/!count!/g, count);
            // append data to row
            row.appendChild(td1);
            row.appendChild(td2);
            row.appendChild(td3);
            row.appendChild(td4);
            count = parseInt(count) + 1;
            // add to count variable
            // append row to table
            tbody.appendChild(row);
        }
        function deleteRow(i) {
            document.getElementById('tblPets').deleteRow(i)
        }
    </script>


Designer Source Code:
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0" class="TemplateTable">
            <tr>
                <td>
                    Add New Row
                </td>
                <td style="float: right; padding-right: 90px;">
                    <a title="Add more template" style="cursor: pointer;" onclick="addRow('tblPets')">
                        <img id="Img1" src="add.png" width="20" />
                        Add Row</a>
                </td>
            </tr>
        </table>
    </div>
    <div style="margin-left: 3%;">
        <table id="tblPets" cellpadding="0" cellspacing="0" class="custom-tablePopup">
            <tr>
                <th>
                    Student Name
                </th>
                <th>
                    Email
                </th>
                <th>
                    Address
                </th>
                <th>
                </th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="SName" size="30" maxlength="30" />
                </td>
                <td>
                    <input type="text" name="Email" size="30" maxlength="30" />
                </td>
                <td>
                    <input type="text" name="Address" size="30" maxlength="30" />
                </td>
                <td>
                    <img src="delete.png" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"
                        style="width: 22px; cursor: pointer;" />
                </td>
            </tr>
        </table>
    </div>
    <div style="margin:30px 0 0 35px;">
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" style="background-color:#00BA8B; padding:10px; color:#fff;"/>
    </div>
    <div>
        <asp:Literal ID="lit" runat="server"></asp:Literal>
    </div>
    </form>
</body>

Code Behind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] SName = Request.Form.GetValues("SName");
        string[] Email = Request.Form.GetValues("Email");
        string[] Address = Request.Form.GetValues("Address");
        DataTable dtable = dt();
        for (int i = 0; i <= SName.Length - 1; i++)
        {
            DataRow row = dtable.NewRow();
            row["SName"] = SName[i];
            row["Email"] = Email[i];
            row["Address"] = Address[i];
            dtable.Rows.Add(row);
        }
        GetData(dtable);
    }

    static DataTable dt()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("SName");
        dt.Columns.Add("Email");
        dt.Columns.Add("Address");

        return dt;
    }

    protected void GetData(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        if (dt.Rows.Count > 0)
        {
            sb.AppendLine("<table cellpadding=\"0\" cellspacing=\"0\" class=\"custom-tablePopup\">");
            sb.AppendLine("<tr><th>Student Name</th>");
            sb.AppendLine("<th>Email</th>");
            sb.AppendLine("<th>Address</th></tr>");
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendLine("<tr>");
                sb.AppendLine("<td>" + dr["SName"].ToString() + "</td>");
                sb.AppendLine("<td>" + dr["Email"].ToString() + "</td>");
                sb.AppendLine("<td>" + dr["Address"].ToString() + "</td>");
                sb.AppendLine("</tr>");
            }
            sb.AppendLine("</table>");
            lit.Text = sb.ToString();
        }
    }
}

No comments:

Post a Comment