Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<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.
            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); 
        }
    }
}
Posted
Updated 2-Nov-14 22:37pm
v3
Comments
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?

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

C#
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;
        }
    }

}
 
Share this answer
 
Hi,

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


Thanks,
Ullas Krishnan
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900