如何以编程方式获取asp.net表列数

时间:2014-08-04 05:38:40

标签: c# asp.net vb.net

我正在使用asp.net表,我必须得到列的数量,我想知道没有任何属性的asp.net表格像grid有" Gridview.columns.count"。 请建议。

2 个答案:

答案 0 :(得分:0)

以下Script将帮助您使用脚本计算动态创建的表的行数或列数。

<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    Dim MyRow as new HTMLTableRow
    Dim MyCell as new HTMLTableCell
    Dim i as Integer
    Dim j as Integer

    Table2.BGColor="Ivory"
    Table2.Border=2
    Table2.BorderColor="LawnGreen"
    Table2.CellPadding=4
    Table2.CellSpacing=3
    Table2.Align="Center"

    MyCell.InnerText = "Column 1"
    MyRow.Cells.Add(MyCell)
    MyCell = New HTMLTableCell
    MyCell.InnerText = "Column 2"
    MyRow.Cells.Add(MyCell)
    Table2.Rows.Add(MyRow)

    For i = 2 to 6
        MyRow = New HTMLTableRow
        For j = 1 to 2
            MyCell = New HTMLTableCell
            MyCell.InnerText = "Cell " & i & ", " & j
            MyRow.Cells.Add(MyCell)
        Next
        Table2.Rows.Add(MyRow)
    Next    

End Sub

Sub Anchor1_Click(Source As Object, E as EventArgs)
    Anchor1.InnerHtml = "Table 1: " & Table1.Rows.Count _
        & " rows - Table 2: " & Table2.Rows.Count & " rows"
End Sub
</SCRIPT>

Anchor1_Click将满足您的要求。

感谢收听我

答案 1 :(得分:0)

我认为表类有.count属性但它支持.net framework 3.0及以上版本见下面的代码片段

//add table first
Table tbl = new Table();
int columnsToAdd = 4;
for (int x = 0; x < columnsToAdd; x++)
tbl.Columns.Add(new TableColumn());
//then get table count
int columns = tbl.Columns.Count;

希望有所帮助