Hi,
To do what you wish, first type a unique identification for each component that you add dynamically, like this:
PnlStudent.Visible = true;
int index_row = 0;
foreach (DataRow dr in dt.Rows)
{
TableRow NewRow1 = new TableRow();
TableCell NewCell1 = new TableCell();
NewCell1.Width = Unit.Pixel(156);
NewCell1.Height = Unit.Pixel(32);
NewCell1.ForeColor = System.Drawing.Color.LightSlateGray;
NewCell1.BackColor = System.Drawing.Color.LightGray;
Label newLable1 = new Label();
newLable1.ID = "mylabel_" + index_row.ToString();
newLable1.Text = dr["Subject"].ToString();
NewCell1.Controls.Add(newLable1);
NewRow1.Cells.Add(NewCell1);
TableCell NewCell2 = new TableCell();
NewCell2.Height = Unit.Pixel(32);
NewCell2.BackColor = System.Drawing.Color.LightGray;
TextBox txtBox1 = new TextBox();
txtBox1.ID = "mytext_" + index_row.ToString();
NewCell2.Controls.Add(txtBox1);
NewRow1.Cells.Add(NewCell2);
tblStudent.Rows.Add(NewRow1);
index_row++;
}
To read the data in the component, do this:
protected void BtnSave_Click(object sender, EventArgs e)
{
string valuemytextbox, valuemylabel;
int index_row = 0;
bool header = true;
foreach (TableRow row in tblStudent.Rows)
{
if (header)
{
header = false;
}
else
{
valuemytextbox = ((TextBox)row.FindControl("mytext_" + index_row.ToString())).Text;
valuemylabel = ((Label)row.FindControl("mylabel_" + index_row.ToString())).Text;
....
index_row++;
}
}
}