Clone an object?

Discussion in 'ASP .Net' started by Alfred Salton, Jun 28, 2004.

  1. 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 Salton, Jun 28, 2004
    #1
    1. Advertisements


  2. 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);
    }
     
    John Saunders, Jun 28, 2004
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.