Hello folks,

I need to create a html table dynamically, so I used HtmlTable and HtmlTableCell ... but I need to put inside a HtmlTableCell another HtmlTable object, so the structure will be:

HtmlTable
|
---HtmlTableRow
|
---HtmlTableCell
|
---HtmlTable

I hope someone could help me

Thanks

Omar

Dani AI

Generated

As described, the goal is to build an HtmlTable in code‑behind and place that table inside a HtmlTableCell. pointed to the core mechanism (adding the inner table as a child control of the cell) and 's Literal suggestion is a useful simpler alternative when the outer layout is fixed. The notes below cover the common server‑side pitfalls and show a concise pattern for keeping nested, dynamic tables working across postbacks.

Dynamic controls must be recreated on every request (ideally during OnInit/Page_Init) so ViewState and event wiring line up. Store the minimal state needed (for example, the number of inner tables or the data used to render them) in ViewState or another durable place, then rebuild the same controls in the same order during Init. Example pattern (VB.NET):

Protected Overrides Sub OnInit(e As EventArgs)
    MyBase.OnInit(e)
    Dim count As Integer = If(ViewState("InnerCount"), 0)
    Dim outerCell = TryCast(FindControl("outerCell"), System.Web.UI.HtmlControls.HtmlTableCell)
    If outerCell IsNot Nothing Then
        For i As Integer = 1 To count
            Dim innerTbl As New System.Web.UI.HtmlControls.HtmlTable()
            innerTbl.ID = "innerTbl_" & i
            Dim r As New System.Web.UI.HtmlControls.HtmlTableRow()
            Dim c As New System.Web.UI.HtmlControls.HtmlTableCell()
            c.InnerText = "Item " & i
            r.Cells.Add(c)
            innerTbl.Rows.Add(r)
            outerCell.Controls.Add(innerTbl)
        Next
    End If
End Sub

Practical tips: give programmatically created controls stable IDs (so FindControl can locate them), rebuild the entire dynamic branch in Init if the outer table is also dynamic, and prefer adding to a named PlaceHolder or server cell declared in markup when possible (simpler lookup and less fragile ordering). For layout-only scenarios, avoid heavy nested tables—use CSS (flex/grid) or a Repeater/GridView for repeatable data instead of manual table building to improve accessibility and maintainability.

The approach recommended by is correct; the main work is ensuring consistent recreation and IDs so ASP.NET can restore ViewState and events.

Recommended Answers

All 8 Replies

If your container table is fixed & you know in which cell you want to put the dynamic conents then i hope literal is best ease to maintain.

<HtmlTable>
<HtmlTableRow>
<HtmlTableCell>
<HtmlTable>
<HtmlTableRow>
<HtmlTableCell>
<HtmlTableCell>
<HtmlTableRow>
<HtmlTable>
<HtmlTableCell>
<HtmlTableRow>
<HtmlTable>

if its not what you want so try to explain your self again

Well... i forgot to say I want to do it, in the codebehind... not in the aspx file...

Well... i forgot to say I want to do it, in the codebehind... not in the aspx file...

What? from the server side?

What? from the server side?

Yes... server side... I've been doing in a simpler maner with

For Each obj As bNoticiasBE In oListaNoticiasBE

            tdvcNotTitular = New System.Web.UI.HtmlControls.HtmlTableCell
            trvcNotTitular = New System.Web.UI.HtmlControls.HtmlTableRow
            tddtNotFecha = New System.Web.UI.HtmlControls.HtmlTableCell
            trdtNotFecha = New System.Web.UI.HtmlControls.HtmlTableRow
            tdvcNotSumilla = New System.Web.UI.HtmlControls.HtmlTableCell
            trvcNotSumilla = New System.Web.UI.HtmlControls.HtmlTableRow

            'avcNotTitular()
            tdvcNotTitular.InnerHtml = "<div align=""justify"" class=""pequeno1""><a href=""wfNoticiasDetalle.aspx?tipo=" + ViewState.Item("pagina.tipo").ToString + "&id=" + obj.inNotCodigo.ToString + """><strong>" + obj.vcNotTitular.ToString + "</strong></a></div>"
            'tdvcNotTitular.InnerText = obj.vcNotTitular
            trvcNotTitular.Cells.Add(tdvcNotTitular)
            tblListaNoticias.Rows.Add(trvcNotTitular)

            tddtNotFecha.InnerHtml = "<strong><font color=""#666666"">" + obj.dtNotFechaNoticia.ToShortDateString + "</font></strong>"
            'tddtNotFecha.InnerText = obj.dtNotFechaNoticia.ToShortDateString
            trdtNotFecha.Cells.Add(tddtNotFecha)
            tblListaNoticias.Rows.Add(trdtNotFecha)

            tdvcNotSumilla.InnerHtml = "<div align=""justify"">" + obj.vcNotSumilla.ToString + "</div>"
            'tdvcNotSumilla.InnerText = obj.vcNotSumilla
            trvcNotSumilla.Cells.Add(tdvcNotSumilla)
            tblListaNoticias.Rows.Add(trvcNotSumilla)

        Next

But I don't know how insert a table, If it can be done...

Next time try to explain your self properly from the begining...;

I will post it in C# code but its not a problem to convert it to VB.

Lets say that you have a HtmlTable ready with everything inside (all the rows and cells that you want, in my code its name is "tbl2".
And the cell that we want to add the table to call "tbl1Cell"
So here is the code:

HtmlTable tbl2 = new HtmlTable();

        HtmlTableCell tbl1Cell = new HtmlTableCell();

        tbl1Cell.Controls.Add(tbl2);

Now convert it to VB

Next time try to explain your self properly from the begining...;

I will post it in C# code but its not a problem to convert it to VB.

Lets say that you have a HtmlTable ready with everything inside (all the rows and cells that you want, in my code its name is "tbl2".
And the cell that we want to add the table to call "tbl1Cell"
So here is the code:

HtmlTable tbl2 = new HtmlTable();

        HtmlTableCell tbl1Cell = new HtmlTableCell();

        tbl1Cell.Controls.Add(tbl2);

Now convert it to VB

Thanks, and I'll try to be more accurate on my explaining.

So, as far I can see, the trick is to construct the object before I add it, I don't know how could I miss it...

Thanks again!...

If you need any more help tell me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.