Create TextBox Dynamically In ASP.NET Using jQuery Read Value

jQuery Is a lightweight, “write less, do more” JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.
In this article, I will show you how to create HTML control dynamically in ASP.NET WebForm and read their values in server-side.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#btnOfficial").click(function() {
                var hdValue = $("#theValue");
                var num = ($("#theValue").val() - 1) + 2;
                var newDiv = $(document.createElement('div')).attr("id", 'my' + num + 'Div');
                newDiv.after().html('<input type="text"  name="TextBox' + num + '" value="TextBox' + num + '" >');


    <form id="form1" runat="server">
    <div id="Div1">
    <asp:GridView ID="GridView1" runat="server">
    <input type="button" id="btnOfficial" value="Add Another TextBox" />
    <input type="hidden" value="1" id="theValue" runat="server" />
    <asp:Button ID="btnSave" runat="server" Text="Read" OnClick="btnSave_Click" />

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.Collections;

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

    protected void btnSave_Click(object sender, EventArgs e)
        List<string> alForm = new List<string>();
        //Textbox id is started
        // with 2 like(TextBox2,TextBox3.....
        for (int i = 2; i < Request.Form.Count - 2; i++)
            string strId = "TextBox" + i.ToString();
            string strValue = Request.Form[strId].ToString();
            strValue = "";

        //Uncomment this line and test.
        //foreach (string x in Request.Form)

        //   string strValue = Request.Form[x].ToString();
        //  alForm.Add(strValue);

        GridView1.DataSource = alForm;

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads