I’m doing an exercise on asp.net using code generated tables with a very simple code:
protected void btnAceptar_Click(object sender, EventArgs e)
{
tblGenerar.Controls.Clear();
for(int i = 0; i < Convert.ToInt32(txtRows.Text);i++)
{
TableRow rowNew = new TableRow();
tblGenerar.Rows.Add(rowNew);
for (int j = 0; j < Convert.ToInt32(txtCols.Text);j++ )
{
TableCell cellNew = new TableCell();
rowNew.Cells.Add(cellNew);
cellNew.Text = txtTexto.Text;
if (chkMargen.Checked == true)
{
cellNew.BorderStyle = BorderStyle.Inset;
cellNew.BorderWidth = 1;
}
}
}
}
The first time I choose to create border on the table, it works, but next time I choose to generate the table without the borders, the borders from last generated table are still there. Additional cells appears with no borders.
Why does this happen if I’m using Controls.Clear() and how can I solve it?
Thanks.
Put else condition within your code.
or you can do something like following.
And you are done.
This is because once your Table is generated you can not apply changes on them and to do that you need to explicitly remove border first and then apply if check box is checked.