2010-06-15 45 views
4

从关于这个问题的MSDN article,我们可以看到,我们创建了一个TableHeaderRow包含TableHeaderCell秒。基于ASP:表控制我们如何创建一个THEAD?

但他们补充表格标题是这样的:

myTable.Row.AddAt(0, headerRow); 

,它输出HTML:

<table id="Table1" ... > 
<tr> 
    <th scope="column" abbr="Col 1 Head">Column 1 Header</th> 
    <th scope="column" abbr="Col 2 Head">Column 2 Header</th> 
    <th scope="column" abbr="Col 3 Head">Column 3 Header</th> 
</tr> 
<tr> 
    <td>(0,0)</td> 
    <td>(0,1)</td> 
    <td>(0,2)</td> 
</tr> 

... 

,它应该有<thead><tbody>(因此它可以无缝使用的tablesorter): )

<table id="Table1" ... > 
<thead> 
<tr> 
    <th scope="column" abbr="Col 1 Head">Column 1 Header</th> 
    <th scope="column" abbr="Col 2 Head">Column 2 Header</th> 
    <th scope="column" abbr="Col 3 Head">Column 3 Header</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td>(0,0)</td> 
    <td>(0,1)</td> 
    <td>(0,2)</td> 
</tr> 
    ... 
    </tbody> 

的HTML代码的aspx是

<asp:Table ID="Table1" runat="server" /> 

我怎样才能输出正确的语法?


正如信息,该GridView控制了这个建在,我们只需要设置Accesbility并使用HeaderRow

gv.UseAccessibleHeader = true; 
gv.HeaderRow.TableSection = TableRowSection.TableHeader; 
gv.HeaderRow.CssClass = "myclass"; 

但问题是,对于Table控制。

回答

2

只是找到一种方法来做到这一点,我们就需要使用从基控件继承我们自己的控件,例如Table

public class myTable : System.Web.UI.WebControls.Table 
{ 
    protected override void OnPreRender(EventArgs e) 
    { 
     Table table = Controls[0] as Table; 

     if (table != null && table.Rows.Count > 0) 
     { 
      // first row is the Table Header <thead> 
      table.Rows[0].TableSection = TableRowSection.TableHeader; 
      // last row is the Footer Header <tfoot> (comment for not using this) 
      table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter; 

      FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic); 
      foreach (TableCell cell in table.Rows[0].Cells) 
       field.SetValue(cell, HtmlTextWriterTag.Th); 
     } 
     base.OnPreRender(e); 
    } 
} 

正常工作与DataGrid以及

public class myDataGrid : System.Web.UI.WebControls.DataGrid 
{ 
    protected override void OnPreRender(EventArgs e) 
    { 
     Table table = Controls[0] as Table; 

     if (table != null && table.Rows.Count > 0) 
     { 
      // first row is the Table Header <thead> 
      table.Rows[0].TableSection = TableRowSection.TableHeader; 
      // last row is the Footer Header <tfoot> (comment for not using this) 
      table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter; 

      FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic); 
      foreach (TableCell cell in table.Rows[0].Cells) 
       field.SetValue(cell, HtmlTextWriterTag.Th); 
     } 
     base.OnPreRender(e); 
    } 
} 

然后例如您只需要使用它来代替基本控件:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     myGridView dg = new myGridView(); 
     dg.DataSource = new string[] { "1", "2", "3", "4", "5", "6" }; 
     dg.DataBind(); 

     ph.Controls.Add(dg); 
    } 
} 

和aspx页面,只需添加一个占位符,如:

<asp:PlaceHolder ID="ph" runat="server" /> 

full example in pastebin

0

我会建议您尝试声明的语法,但现在我知道你的意思;下面的代码...:

<asp:Table runat="server" ID="sometable"> 
    <asp:TableHeaderRow BackColor="#ADD9E6"> 
     <asp:TableHeaderCell ID="DataHeader" CssClass="Heading1" Text="Data Header" /> 
    </asp:TableHeaderRow> 
    <asp:TableRow> 
     <asp:TableCell>Data</asp:TableCell> 
    </asp:TableRow> 
</asp:Table> 

...产生相似的结果给你贴什么:

<table id="sometable" border="0"> 
<tr style="background-color:#ADD9E6;"> 
    <th id="DataHeader" class="Heading1">Data Header</th> 
</tr><tr> 
    <td>Data</td> 
</tr> 
</table> 

也许自定义控件可以创建来解决问题了吗?好像矫枉过正,但至少你可以控制生成的输出是什么。

另一种选择是从System.Web.UI.WebControls.Table类,并override how the table is rendered继承。

+0

我不能使用声明语法:(这是一个动态表,只有运行时我知道有多少什么是什么的列全部。 ..这个想法使用由设计者生成的XML来创建“Grid” – balexandre 2010-06-15 12:50:02

0

如果你可以用一个DataGrid时,你的名字在模板中的列会产生一个THEAD和TBODY适当。

+0

DataGrid默认不会这样做 – balexandre 2010-06-15 16:30:11

1

请注意:这是一个VB.net回答同样的问题,你可以简单地将它转换为C#使用任意数量的免费在线转换器。

用一个thead & tbody创建一个简单的HTML表格。我特别需要用于jQuery tablesorter插件的thead元素。

Dim ta As Table 
    Dim thr As TableHeaderRow 
    Dim thc As TableHeaderCell 
    Dim tr As TableRow 
    Dim td As TableCell 

    ta = New Table 
    ' make a header row & explicitly place it into the header section 
    thr = New TableHeaderRow 
    thr.TableSection = TableRowSection.TableHeader 
    ' add cells to header row 
    thc = New TableHeaderCell 
    thc.Text = "ID" 
    thr.Cells.Add(thc) 
    thc = New TableHeaderCell 
    thc.Text = "DESC" 
    thr.Cells.Add(thc) 
    ' create a data row & append cells to it 
    tr = New TableRow 
    td = New TableCell 
    td.Text = "101" 
    tr.Cells.Add(td) 
    td = New TableCell 
    td.Text = "VB.net is not so bad." 
    tr.Cells.Add(td) 
    ' append the header & row to the table 
    ta.Rows.Add(thr) 
    ta.Rows.Add(tr) 

    ' add the new table to your page 
    Page.Controls.Add(ta) 
+0

你没有得到这个问题,如果你有一个数据源,为什么我想要手工操作所有东西?计算列数,行数等,如果我可以简单地执行'myTable.DataSource = myDataSource;'并且我想要的只是创建e''就像其他一切已经在那里一样! – balexandre 2012-01-27 20:54:16

1

我在这上百万年晚了,但我只是需要做同样的事情。它的处理和网格视图一样。

 foreach (var item in dynamicData) 
     { 
      var c = new TableHeaderCell() 
      c.Text = item.Whatever; 
      hr.Cells.Add(c); 
     } 
     //This is the important line 
     hr.TableSection = TableRowSection.TableHeader; 

     MyTable.Rows.Add(hr); 
0

添加TableSection="TableHeader"到TableHeaderRow,如:

<asp:TableHeaderRow ID="TableHeaderRow2" 
runat="server" TableSection="TableHeader" > 
相关问题