2009-05-22 34 views
0

在ASP.Net表或HtmlTable中是否存在equivilent,以在HTML中將元素設置爲將樣式或類應用於整個列中的<col> </col >/col >?理想情況下,我期望以WebPart而不是ASP.Net設計器的方式編程。ASP.Net HtmlTable - 任何等價於<col></col>?

回答

0

的HTMLTABLE可能包含HtmlGenericControls,它給你一個山坳:

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

它不工作,在.NET 4。 – 2011-01-03 23:47:48

0

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(v=VS.100).aspx,該<col>不HTMLTABLE支持。所以唯一的方法就是覆蓋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")) 
0

這裏是呈現爲每列<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; 
    } 
} 
相關問題