ASP.Net HtmlTable - 任何等效的?

时间:2009-05-22 13:21:33

标签: asp.net

ASP.Net表或HtmlTable中的< col>< / col>是否存在等效值HTML中的元素集将样式或类应用于整个列?理想情况下,我希望以编程方式为WebPart而不是ASP.Net设计器执行此操作。

3 个答案:

答案 0 :(得分:0)

HtmlTable可以包含HtmlGenericControls,它会为您提供col:

var t = new HtmlTable();
t.Controls.Add(new HtmlGenericControl("col"));

答案 1 :(得分:0)

根据http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(v=VS.100).aspx,HtmlTable不支持<col>。因此,唯一的方法是覆盖RenderChildren()方法。

Private Class MyHtmlTable
    Inherits HtmlTable

    Private _tableDefinitionCells As New ControlCollection(Me)
    Public ReadOnly Property TableDefinitionCells As ControlCollection
        Get
            Return Me._tableDefinitionCells
        End Get
    End Property

    Protected Overrides Sub RenderChildren(ByVal writer As System.Web.UI.HtmlTextWriter)
        For Each c In Me._tableDefinitionCells.OfType(Of Control)()
            c.RenderControl(writer)
        Next

        MyBase.RenderChildren(writer)
    End Sub

End Class

你可以像

一样使用它
Dim table = New MyHtmlTable()
table.TableDefinitionCells.Add(New HtmlGenericControl("col"))

答案 2 :(得分:0)

这是Table的子类,为每列呈现<col />个标记:

public sealed class ColTable : Table
{
    protected override void RenderContents(HtmlTextWriter writer)
    {
        for (int i = 0; i < GetColumnCount(); i++)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Col);
            writer.RenderEndTag();
        }
        base.RenderContents(writer);
    }

    private int GetColumnCount()
    {
        int maxCount = 0;
        foreach (TableRow row in Rows)
        {
            int count = 0;
            foreach (TableCell cell in row.Cells)
            {
                count += Math.Max(1, cell.ColumnSpan);
            }
            maxCount = Math.Max(count, maxCount);
        }
        return maxCount;
    }
}