views:

66

answers:

1

how to copy table controls?

table2=table1?

asp.net C# vs 08 table control

reason, assume the below strings to be tables.

string full; string userinput

full=full+userinput;

so,?

+3  A: 

No you can't. Table like any other control, is a reference type. It means that copying it just copies a reference to the real object instance. Because it doesn't implement System.ICloneable you have to create a new one and then copy properties manually one by one.

I would be curious to know why you are trying to do this, because it doesn't seem to follow any of the best practices that I am familiar with. Could you describe what you are trying to do?

One thing you could do is copy the contents of a table, although this won't copy the other properties such as styles and cell-padding etc:

        protected void CopyTable()
        {
            var clontable= new HtmlTable();
            var mytbl = form1.FindControl("mytable") as HtmlTable;
            if (mytbl != null)
            {
                HtmlTableRow myrow;
                HtmlTableCell mycell;

                for (int i = 0; i < mytbl.Rows.Count - 1; i++)
                {
                    myrow = new HtmlTableRow();
                    for (int j = 0; j < mytbl.Rows[i].Cells.Count - 1; j++)
                    {
                        mycell = new HtmlTableCell();
                        mycell.InnerHtml = mytbl.Rows[i].Cells[j].InnerHtml;
                        myrow.Cells.Add(mycell);
                    }
                    clontable.Rows.Add(myrow);
                }
                form1.Controls.Add(clontable);
            }
        }
Daniel Dyson
Might be a System.Web.UI.WebControls.Table.
Martin Smith
In which case you would just drop the Html from HtmlTable, HtmlRiow and HtmlCell and change the InnerHtml property to Text. Then it will do the same.
Daniel Dyson
Thank you so much both for code and for point it for a asp.net table control. thank you
No worries. Good luck
Daniel Dyson