How to Dynamically create textbox in asp.net using C Sharp

  ASP.NET   Tuesday, May 19, 2015   4149 | No Comments Yet!!! | No Download | No Demo | Pawan kumar
 Tags: Dynamically create textbox in asp.net using C# with example  Dynamically create textbox  create textbox in asp.net using C# with example  create textbox in asp.net using C# 


In this tutorial, We are going to learn to Dynamically create textbox in asp.net using C# with example

Step 1: Create a asp.net empty Web Application

Step 2: Create a webform “Default.aspx” and write the following codes.

 <!DOCTYPE html>
<html>
<head runat="server">
    <title>Dynamically create textboxes in ASP.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    No of Text boxes
                </td>
                <td>
                    <asp:TextBox ID="txtNumbers" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
                </td>
        </table>
        <br />
      
    </div>
    </form>
</body>
</html>

Step 3: Write the below codes in the C# Code Behind

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DynamicControls : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int noofcontrols = Convert.ToInt32(txtNumbers.Text);
        for (int i = 1; i <= noofcontrols; i++)
        {
            TextBox NewTextBox = new TextBox();
            NewTextBox.ID = "TextBox" + i.ToString();
            NewTextBox.Style["Clear"] = "Both";
            NewTextBox.Style["Float"] = "Left";
            NewTextBox.Style["Top"] = "25px";
            NewTextBox.Style["Left"] = "100px";
            //form1 is a form in my .aspx file with runat=server attribute
            form1.Controls.Add(NewTextBox);
        }
    }
}

Step 3:  Run the application or press the F5 Key on the keyboard. You will get the following output as below to Dynamically create textbox in asp.net using C# with example

Conclusion:-
so In this tutorial, we have learned how to Dynamically create textbox in asp.net using C# with example. Happy Coding!!!!

Post Comments

if you have some suggestion, question, request on How to Dynamically create textbox in asp.net using C Sharp.

Please use the comment box to express your views.

 

Comments: