在asp:Table Control我们如何创建thead?
从关于该主题的MSDN文章中 ,我们可以看到我们创建了一个包含TableHeaderCell
。
但是他们像这样添加表头:
myTable.Row.AddAt(0, headerRow);
输出HTML:
Column 1 Header Column 2 Header Column 3 Header (0,0) (0,1) (0,2) ...
它应该有
和
(所以它与tablesorter无缝地工作) 🙂
Column 1 Header Column 2 Header Column 3 Header (0,0) (0,1) (0,2) ...
HTML aspx代码是
如何输出正确的语法?
就像信息一样 , GridView
控件内置了这个,因为我们只需要设置Accesbility并使用HeaderRow
gv.UseAccessibleHeader = true; gv.HeaderRow.TableSection = TableRowSection.TableHeader; gv.HeaderRow.CssClass = "myclass";
但问题是Table
控件。
刚刚找到了这样做的方法,我们确实需要使用我们自己的从基本控件inheritance的控件,例如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 table.Rows[0].TableSection = TableRowSection.TableHeader; // last row is the Footer Header (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 table.Rows[0].TableSection = TableRowSection.TableHeader; // last row is the Footer Header (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页面中,只需添加一个占位符,如:
在pastebin中的完整示例
请注意:这是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)
我已经晚了一百万年,但我只需要做同样的事情。 它的处理方式与网格视图大致相同。
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);
我打算建议你尝试声明性语法,但我现在知道你的意思了; 以下代码……:
Data
…产生的结果与您发布的结果相似:
Data Header Data
也许可以创建自定义控件来解决您的问题? 看起来像矫枉过正,但至少你可以控制生成的输出是什么。
另一种选择是从System.Web.UI.WebControls.Table
类inheritance,并覆盖表的呈现方式 。
如果在命名模板中的列时可以使用DataGrid,它将适当地生成thead和tbody。
将TableSection="TableHeader"
添加到TableSection="TableHeader"
,如:
- 如何在C#中对ResourceSet进行排序
- 需要解决.Net Master Page Name Mangling的问题
- 获取错误消息 – 无法将类型’string’隐式转换为’bool’
- .net在运行时确定我的应用程序是exe还是Web应用程序
- 在ASP.Net的ListView中分组
- 多次调用同一Web服务阻止?
- 是否有可能加速这种方法?
- ASP.NET JQuery AJAX POST返回数据但在401响应中
- 代码Behing更新web.config映射问题
Interesting Posts