ASP.Net - 静态 TableHeaderRow 和动态 Tablerows,THR 消失和 CSS 问题

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

这里有两个问题。

1) 静态 TableHeaderRow 已消失

这是我的 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>

在页面加载事件中,我正在调用一个子例程来动态创建 table 行、table 单元格、各种控件并填写值。

这是该子程序的摘录:

        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

表格行和控件以正确的方式创建和填充,一切都按预期工作,但 TableHeaderRow(应该是静态的)没有出现。我想避免动态创建它,因为它没有多大意义。谁能解释一下为什么会这样?

2) tbEdit CSS 被忽略

最初我的 table tbEdit 是一个简单的客户端 html table 我安排了以下 CSS 代码(工作正常):

    #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;
    } 
   

然后我只好把tbEdit改成一个ASP控件,从那时起具体的CSS就被忽略了,完全没有考虑。为了将它绑定到 ASP 控件,我应该在 CSS 中更改什么吗?

提前感谢您的宝贵时间和帮助。

此致,卢卡

我选择使用 属性 行和单元格的样式来动态属性 CSS。

像这样:

        '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