Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<protected void="" eventargs="" mode="hold" />        {

            if (!Page.IsPostBack)
                Session.Remove("clicks");
        }

       
        protected void Button2_Click1(object sender, EventArgs e)
        {
            int rowcount = 0;
            rowcount = Convert.ToInt32(Session["clicks"]);
            rowcount++;
            Session["clicks"] = rowcount;
            for (int i = 0; i < rowcount; i++)
            {
                TextBox TextBox1 = new TextBox();
                TextBox TextBox2 = new TextBox();
                TextBox TextBox3 = new TextBox();

                Label Label1 = new Label();
                Label Label2 = new Label();
                Label Label3 = new Label();
                TextBox1.ID = "TextBox1" + i.ToString();
                TextBox2.ID = "TextBox2" + i.ToString();
                TextBox3.ID = "TextBox3" + i.ToString();

                Label1.ID = "Label1" + i.ToString();
                Label2.ID = "Label2" + i.ToString();
                Label3.ID = "Label3" + i.ToString();
                Label1.Text = "name" + (i + 1).ToString() + " :";
                Label2.Text="phoneno  :";
                Label3.Text = "emailid:";
                
                Panel1.Controls.Add(Label1);
                Panel1.Controls.Add(TextBox1);
                
                Panel1.Controls.Add(Label2);
                Panel1.Controls.Add(TextBox2);

                Panel1.Controls.Add(Label3);
                Panel1.Controls.Add(TextBox3);
                Panel1.Controls.Add(Button2);
                Panel1.Controls.Add(Button1);
                
                

            }
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {}>



which code i'am using to successfully saved that text boxes data.
please give me solution i'am fresher.

What I have tried:

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (TextBox textBox in Panel1.Controls.OfType<textbox>())
            {
                foreach (TextBox txtDynamic in Panel1.Controls.OfType<textbox>())
                {
                    foreach (TextBox txtDynamic1 in Panel1.Controls.OfType<textbox>())
                    {
                        SqlConnection con = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=sa;Password=123");
                        SqlCommand cmd = new SqlCommand("insert into multipledata values (@name,@phoneno,@emailid)", con);
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@name", textBox.Text);
                        cmd.Parameters.AddWithValue("@phoneno", txtDynamic.Text);
                        cmd.Parameters.AddWithValue("@emailid", txtDynamic1.Text);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Clear();
                    }
                }
            }

        }</textbox></textbox></textbox>
Posted
Updated 28-Apr-16 22:00pm
v2

1 solution

use FindControl[^]

usage:

C#
protected void Page_Load(object sender, EventArgs ee)
       {

           Label label = new Label() { Text = "Name:" };
           TextBox textbox = new TextBox() { ID = "txtboxName" };
           Panel1.Controls.Add(label);
           Panel1.Controls.Add(textbox);

       }




       protected void button1_Click(object sender, EventArgs e)
       {
          TextBox textbox = Panel1.FindControl("txtboxName") as TextBox;
          string text = textbox.Text;
       }
 
Share this answer
 
Comments
Nagarjuna99 29-Apr-16 2:52am    
sir,
in that code newly created 1 row data only saving.
i need multiple rows of data will be save in database?
thanks.
Karthik_Mahalingam 29-Apr-16 4:01am    
use this code

protected void Page_Load(object sender, EventArgs ee)
{

int rowcount = 5;

for (int i = 0; i < rowcount; i++)
{
Label label = new Label() { Text = "Name:" + i.ToString() };
TextBox textbox = new TextBox() { ID = "txtboxName" + i.ToString() };
Label label1 = new Label() { Text = "phoneno:" + i.ToString() };

TextBox textbox1 = new TextBox() { ID = "txtboxphoneno" + i.ToString() };
Label label2 = new Label() { Text = "emailid:" + i.ToString() };
TextBox textbox2 = new TextBox() { ID = "txtboxemailid" + i.ToString() };

Panel1.Controls.Add(label);
Panel1.Controls.Add(textbox);
Panel1.Controls.Add(label1);
Panel1.Controls.Add(textbox1);
Panel1.Controls.Add(label2);
Panel1.Controls.Add(textbox2);

}
}

protected void Button2_Click(object sender, EventArgs e)
{
int rowcount = 5;
for (int i = 0; i < rowcount; i++)
{


string text = "", text1 = "", text2 = "";
TextBox textbox = Panel1.FindControl("txtboxName" + i.ToString()) as TextBox;
TextBox textbox1 = Panel1.FindControl("txtboxphoneno" + i.ToString()) as TextBox;
TextBox textbox2 = Panel1.FindControl("txtboxemailid" + i.ToString()) as TextBox;
if (textbox != null)
text = textbox.Text;

if (textbox1 != null)
text1 = textbox1.Text;

if (textbox2 != null)
text2 = textbox2.Text;

SqlConnection con = new SqlConnection("Data Source=XENORIX8-PC;Initial Catalog=xenorix;User ID=sa;Password=123");
SqlCommand cmd = new SqlCommand("insert into multipledata values(@name,@phoneno,@emailid)", con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@name", text);
cmd.Parameters.AddWithValue("@phoneno", text1);
cmd.Parameters.AddWithValue("@emailid", text2);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Nagarjuna99 29-Apr-16 4:37am    
thanks so match sir its working.....
Karthik_Mahalingam 29-Apr-16 4:39am    
welcome :)

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