Click here to Skip to main content
14,427,798 members
Rate this:
Please Sign up or sign in to vote.
See more:
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

my default.aspx.cs file 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 test_Default2 : System.Web.UI.Page
    protected void Page_load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            //Remove the session when first time page loads.

    protected void Button1_Click(object sender, EventArgs e)
        int rowCount = 0;
        //initialize a session.
        rowCount = Convert.ToInt32(Session["clicks"]);
        //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.
Updated 2-Nov-14 23:37pm
Sinisa Hajnal 3-Nov-14 4:34am
Sooo, what is your question? You don't show any database saving code that doens't work...what have you tried before now?
Rate this:
Please Sign up or sign in to vote.

Solution 1

in the submit button click use the following code to retrieve texts from dynamically created textboxes

protected void btnSubmit_Click(object sender, EventArgs e)
    TextBox txt;

    foreach (Control c in Panel1.Controls)
        if (c.GetType() == typeof(TextBox))
            txt = (TextBox)c;
            String str = txt.Text;

Rate this:
Please Sign up or sign in to vote.

Solution 2


Assign unique ID for each Textbox control.... for unique id use GUID generator

Please store the (dynamic) id of text box in a List<string> and then access the dynamically created text-box by its name...

Ullas Krishnan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100