ASP.Net HtmlTable - Any equivalent to <col> </col>?

Is there an equivalent value in an ASP.Net table or HtmlTable for a <col> </col> element in HTML to apply a style or class to the entire column? Ideally I want to do this programmatically for the WebPart and not in the ASP.Net designer.

0


a source to share


3 answers


The HtmlTable can contain HtmlGenericControls which gives you the command:



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

      

0


a source


According to http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(v=VS.100).aspx , <col>

not supported in HtmlTable. Thus, the only way to do this is to override the method 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

      



You can use this like

Dim table = New MyHtmlTable()
table.TableDefinitionCells.Add(New HtmlGenericControl("col"))

      

0


a source


Here is a subclass of a table that displays tags <col />

for each column:

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

      

0


a source







All Articles