基于ASP:表控制我们如何创建一个THEAD? [英] On asp:Table Control how do we create a thead?
问题描述
从 MSDN文章关于这个问题,我们可以看到,我们创建了一个 TableHeaderRow
包含 TableHeaderCell
秒。
不过,他们补充表头是这样的:
myTable.Row.AddAt(0,headerRow );
其输出HTML:
<表ID =表1...>
< TR>
百分位范围=列简称=1上校团长>第1列页眉和LT; /第i
百分位范围=列简称=2上校团长>列2部< /第i
百分位范围=列简称=西3头>第3栏页眉和LT; /第i
< / TR>
< TR>
< TD>(0,0)LT; / TD>
< TD>(0,1)LT; / TD>
< TD>(0,2)LT; / TD>
< / TR>
...
和它应该有 < THEAD>
和< TBODY>
的(因此它可以无缝使用的tablesorter)的:)
<表ID =表1...>
<&THEAD GT;
< TR>
百分位范围=列简称=1上校团长>第1列页眉和LT; /第i
百分位范围=列简称=2上校团长>列2部< /第i
百分位范围=列简称=西3头>第3栏页眉和LT; /第i
< / TR>
< / THEAD>
<&TBODY GT;
< TR>
< TD>(0,0)LT; / TD>
< TD>(0,1)LT; / TD>
< TD>(0,2)LT; / TD>
< / TR>
:
< / TBODY>
的
HTML ASPX代码
< ASP:表ID =表1=服务器/>
我怎样才能输出正确的语法?
正如信息的 GridView控件
控件还具有以下内置作为我们只需要设置Accesbility并使用 HeaderRow
gv.UseAccessibleHeader = TRUE;
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
gv.HeaderRow.CssClass =MyClass的;
但问题是表
控制
只是找到了一个办法做到这一点,我们需要使用从基本控制继承我们自己的控制,对例如表
公共类myTable的:System.Web.UI程序。 WebControls.Table
{
保护覆盖无效的OnPreRender(EventArgs的发送)
{
表表=控制[0]如表;
如果(表=空&安培;!&安培; table.Rows.Count大于0)
{
//第一行是表头<&THEAD GT;
table.Rows [0] = .TableSection TableRowSection.TableHeader;
//最后一行是页脚页眉和LT; TFOOT>
table.Rows(不使用对此有何评论)[table.Rows.Count - 1] .TableSection = TableRowSection.TableFooter;
字段信息字段= typeof运算(的WebControl).GetField(tagKey,BindingFlags.Instance | BindingFlags.NonPublic可);
的foreach(在table.Rows [0] .Cells TableCell的细胞)
field.SetValue(细胞,HtmlTextWriterTag.Th);
}
base.OnPreRender(E);
}
}
正常工作与的DataGrid
以及
公共类myDataGrid:System.Web.UI.WebControls.DataGrid
{
保护覆盖无效的OnPreRender(EventArgs的发送)
{
表表=控制[0]如表;
如果(表=空&安培;!&安培; table.Rows.Count大于0)
{
//第一行是表头<&THEAD GT;
table.Rows [0] = .TableSection TableRowSection.TableHeader;
//最后一行是页脚页眉和LT; TFOOT>
table.Rows(不使用对此有何评论)[table.Rows.Count - 1] .TableSection = TableRowSection.TableFooter;
字段信息字段= typeof运算(的WebControl).GetField(tagKey,BindingFlags.Instance | BindingFlags.NonPublic可);
的foreach(在table.Rows [0] .Cells TableCell的细胞)
field.SetValue(细胞,HtmlTextWriterTag.Th);
}
base.OnPreRender(E);
}
}
再比如你只需要而不是用它基地控制:
保护无效的Page_Load(对象发件人,EventArgs五)
{
如果(页! .IsPostBack)
{
myGridView DG =新myGridView();
dg.DataSource =新的字符串[] {1,2,3,4,5,6};
dg.DataBind();
ph.Controls.Add(DG);
}
}
和在aspx页面,只需添加像一个占位符
< ASP:占位符ID =PH=服务器/>
块引用>From an MSDN article on the subject, we can see that we create a
TableHeaderRow
that containsTableHeaderCell
s.But they add the table header like this:
myTable.Row.AddAt(0, headerRow);
which outputs the 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> ...
and it should have
<thead>
and<tbody>
(so it works seamless with 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>
the HTML aspx code is
<asp:Table ID="Table1" runat="server" />
How can I output the correct syntax?
Just as information, the
GridView
control has this built in as we just need to set the Accesbility and use theHeaderRow
gv.UseAccessibleHeader = true; gv.HeaderRow.TableSection = TableRowSection.TableHeader; gv.HeaderRow.CssClass = "myclass";
but the question is for the
Table
control.解决方案Just found a way to do this, we do need to use our own controls that inherit from the base control, for example a
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); } }
works fine with
DataGrid
as wellpublic 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); } }
then for example you just need to use it instead the base control:
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); } }
and in aspx page, just add a place holder like:
<asp:PlaceHolder ID="ph" runat="server" />
这篇关于基于ASP:表控制我们如何创建一个THEAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!