cssasp.netvb.nettableheader

ASP.Net - Static TableHeaderRow and Dynamic Tablerows, THR disappeared and CSS problem


I've got two problems here.

1) Static TableHeaderRow has disappeared

This is my table:

        <asp:table id="tbEdit" runat="server" style="width: 100%;">
            <asp:TableHeaderRow id="th_Row">
                <asp:TableHeaderCell Scope="Column" Text="Scope" />
                <asp:TableHeaderCell Scope="Column" Text="Site ID" />
                <asp:TableHeaderCell Scope="Column" Text="Site Name" />
                <asp:TableHeaderCell Scope="Column" Text="Address" />
                <asp:TableHeaderCell Scope="Column" Text="CAP/ZIP" />
                <asp:TableHeaderCell Scope="Column" Text="City" />
                <asp:TableHeaderCell Scope="Column" Text="Country" />
            </asp:TableHeaderRow>
        </asp:table>

In the page load event I'm calling a subroutine to dynamically create tablerows, tablecells, various controls and fill in the values.

This is an extract of that subroutine:

        For y = 0 To i
            While myReader.Read

                Dim tr As New TableRow
                tbEdit.Rows.Add(tr)
                tr.ID = "tr" & y
                For z = 0 To 6
                    Select Case z
                        Case = 0
                            Dim tc As New TableCell
                            Dim ddl As New DropDownList
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(ddl)
                            ddl.ID = "ddlScope" & y & "-" & z
                            For x = 0 To iScopes
                                ddl.Items.Add(New ListItem(Scope(x).Name, Scope(x).ID.ToString))
                            Next
                            ddl.SelectedValue = myReader.Item(1)

                        Case = 1
                            Dim tc As New TableCell
                            Dim lbl As New Label
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(lbl)
                            lbl.ID = "lblSid" & y & "-" & z
                            lbl.Text = myReader.Item(2)

                        Case = 2
                            Dim tc As New TableCell
                            Dim tbx As New TextBox
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(tbx)
                            tbx.ID = "tbxSname" & y & "-" & z
                            tbx.Text = myReader.Item(3)

                        Case = 3
                            Dim tc As New TableCell
                            Dim tbx As New TextBox
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(tbx)
                            tbx.ID = "tbxAddress" & y & "-" & z
                            tbx.Text = myReader.Item(4)

                        Case = 4
                            Dim tc As New TableCell
                            Dim tbx As New TextBox
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(tbx)
                            tbx.ID = "tbxCap" & y & "-" & z
                            tbx.Text = myReader.Item(5)

                        Case = 5
                            Dim tc As New TableCell
                            Dim tbx As New TextBox
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(tbx)
                            tbx.ID = "tbxCity" & y & "-" & z
                            tbx.Text = myReader.Item(6)

                        Case = 6
                            Dim tc As New TableCell
                            Dim ddl As New DropDownList
                            tbEdit.Rows.Item(y).Cells.Add(tc)
                            tbEdit.Rows.Item(y).Cells(z).Controls.Add(ddl)
                            ddl.ID = "ddlCountry" & y & "-" & z
                            For x = 0 To iCountries
                                ddl.Items.Add(New ListItem(Country(x).Name, Country(x).ID.ToString))
                            Next
                            ddl.SelectedValue = myReader.Item(7)

                    End Select
                Next
            End While
        Next

Tablerows and controls are created and filled the correct way, everything works as expected but the fact that the TableHeaderRow (that is supposed to be static), doesn't appear. I would like to avoid to create it dynamically since it doesn't make much sense. Can anyone explain the reason why this happens?

2) tbEdit CSS are ignored

Originally my table tbEdit was a simple client side html table and I had the following CSS code arranged (it was working fine):

    #tbEdit table { 
            /* border-collapse: collapse; */
            width: 100%; 
            border: 1px solid #868583;
    } 
          
    #tbEdit th, #tbEdit td { 
            text-align: center; 
            padding: 8px;
            border: 1px solid #868583;
    } 
          
    #tbEdit tr:nth-child(1) { 
            background-color: #5D7B9D;
            text-align: center; 
            vertical-align: middle;
            color: white;
            border: 1px solid #868583;
    } 
    #tbEdit tr:nth-child(even):not(:first-child) { 
            background-color: #F7F6F2;
            border: 1px solid #868583;
    } 
   

Then I had to transform tbEdit into an ASP control, from that point on the specific CSS has been ignored, totally not taken into consideration. Is there anything I should change in my CSS in order to bind it to an ASP control?

Thanks in advance for your time and help.

Regards, Luca


Solution

  • I have opted to use the style property of rows and cells to dynamically attribute the CSS.

    Like this:

            'CSS TableEdit
            With tbEdit.Style
                .Add("width", "100%")
                .Add("border", "1px solid #868583")
                .Add("text-align", "center")
                .Add("padding", "8px")
            End With
    
            'CSS TableHeaderRow
            With tbEdit.Rows(0).Style
                .Add("background-color", "#5D7B9D")
                .Add("text-align", "center")
                .Add("vertical-align", "center")
                .Add("color", "white")
                .Add("border", "1px solid #868583")
            End With
    
            'CSS TableHeaderCells
            For w = 0 To 6
                With tbEdit.Rows(0).Cells(w).Style
                    .Add("text-align", "center")
                    .Add("padding", "8px")
                    .Add("border", "1px solid #868583")
                End With
            Next