Clone an object?

A

Alfred Salton

Can anyone show me a simple way to clone an object? In this case, I
have an HTMLTable object which has a hidden header row acting as a
template. I would like to clone the header row and add the new row to
the table. This is trivially simple in javascript using the DOM - does
an equally simple method exist in asp.net code?
 
J

John Saunders

Alfred Salton said:
Can anyone show me a simple way to clone an object? In this case, I
have an HTMLTable object which has a hidden header row acting as a
template. I would like to clone the header row and add the new row to
the table. This is trivially simple in javascript using the DOM - does
an equally simple method exist in asp.net code?


Alfred, your question isn't actually a general "clone an object" question,
because the answer to that one is, "if the object implements IClonable, then
just call IClonable.Clone()".

As it is, you'll have to do it manually. For instance, assuming your .aspx
page has:

<table runat="server" id="myTable">
<tr runat="server" id="myHeaderRow">
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>

then you'll have to do something like this:

protected System.Web.UI.HtmlControls.HtmlTableRow myHeaderRow;
protected System.Web.UI.HtmlControls.HtmlTable myTable;
private void Page_Load(object sender, System.EventArgs e)
{
HtmlTableRow newHeaderRow = new HtmlTableRow();
newHeaderRow.ID = "newHeaderRow";
newHeaderRow.Height = myHeaderRow.Height; // etc

foreach (HtmlTableCell cell in myHeaderRow.Cells)
{
HtmlTableCell newCell = new HtmlTableCell();
newCell.ID = cell.ID;
newCell.Width = cell.Width; // etc
newHeaderRow.Cells.Add(newCell);
}

myTable.Rows.Insert(1, newHeaderRow);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top