top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

    Get Free Article Updates

Facebook Login
Site Registration

C#.Net - Dynamically Create Textbox In Asp.Net

+4 votes
114 views

Aspx Code:

<!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>

 

 C#.Net Code:

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);

        }

    }

}

 

Equivalent VB.Net Code:

Imports System.Collections.Generic

Imports System.Linq

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

 

Partial Public Class DynamicControls

    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)

        Dim noofcontrols As Integer = Convert.ToInt32(txtNumbers.Text)

        For i As Integer = 1 To noofcontrols

            Dim NewTextBox As 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)

        Next

    End Sub

End Class

  
  

 

posted Nov 6, 2015 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Introduction

This is about creating controls dynamically using C# in web pages. So I have considered Textboxes and labels to develop this.

Important Tasks

  1. Creating the text boxes when button is clicked.
  2. Creating text boxes as numbers are predefined.

Example

 

Discussed 2 examples. One I crated textboxes when the button is created. Here we can create any number of textboxes. In the second example we created textboxes inside a table when the row and columns are specified.

 

Demonstration

Creating the textboxes when button is clicked each time

 

image



Creating text boxes as numbers are predefined.

 

image

 

Implementation

Creating the textboxes when button is clicked each time

Here I have created the text boxes using C# in the code behind. The controls are created in each button click.

Code Snippet

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


public partial class MultipleTextBox : System.Web.UI.Page
{

    protected void Page_load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Remove the session when first time page loads.
            Session.Remove("clicks");
        }


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int rowCount = 0;

        //initialize a session.
        rowCount = Convert.ToInt32(Session["clicks"]);

        rowCount++;

        //In each button clic save the numbers into the session.
        Session["clicks"] = rowCount;


        //Create the textboxes and labels each time the button is clicked.
        for (int i = 0; i < rowCount; i++)
        {

            TextBox TxtBoxU = new TextBox();
        
            TextBox TxtBoxE = new TextBox();

            Label lblU = new Label();
            Label lblE = new Label();

            TxtBoxU.ID = "TextBoxU" + i.ToString();
            TxtBoxE.ID = "TextBoxE" + i.ToString();

            lblU.ID = "LabelU" + i.ToString();
            lblE.ID = "LabelE" + i.ToString();


            lblU.Text = "User " + (i + 1).ToString() + " : ";
            lblE.Text = "E-Mail : ";

            //Add the labels and textboxes to the Panel.
            Panel1.Controls.Add(lblU);
            Panel1.Controls.Add(TxtBoxU);

            Panel1.Controls.Add(lblE);
            Panel1.Controls.Add(TxtBoxE);
        

        }


    }

}



Creating text boxes as numbers are predefined.

Here I have created a table using C# and other controls as well.

Code Snippet

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

public partial class AddDefinedTextBox : System.Web.UI.Page
{

    protected void Button1_Click1(object sender, EventArgs e)
    {


        int rowCount = Convert.ToInt32(TextBox1.Text);

        int columnCount = Convert.ToInt32(TextBox2.Text);

        Table table = new Table();

        table.ID = "table1";


        for (int i = 0; i < rowCount; i++)
        {

            TableRow row = new TableRow();

            for (int j = 0; j < columnCount; j++)
            {

                TableCell cell = new TableCell();

                TextBox TxtBoxU = new TextBox();


                TxtBoxU.ID = "TextBoxU" + i.ToString();

                cell.ID = "cell" + i.ToString();

                cell.Controls.Add(TxtBoxU);

                row.Cells.Add(cell);


            }


            table.Rows.Add(row);

        }


        Panel1.Controls.Add(table);

    }
}

READ MORE
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...