使用System.Web.UI.WebControls向表添加标签

时间:2014-07-30 12:23:26

标签: c#

我需要以某种方式将<thead>标记添加到我的循环中。 tableHEaderRow类仅使用<tr><th>标记,我需要将它们嵌套在<thead>标记中以用于样式目的。

我尝试过使用各种方法,包括字符串编写器和文字控件,但我似乎无法使其工作。

TableHeaderRow thr = new TableHeaderRow();
tbl.Controls.Add(new LiteralControl("<thead>")); // This is the line I need to add
foreach (DataColumn col in dt.Columns)
{
  TableHeaderCell th = new TableHeaderCell();
  th.Text = col.Caption;
  thr.Controls.Add(th);
}
tbl.Controls.Add(new LiteralControl("</thead>")); // This is the line I need to add
tbl.Controls.Add(thr);
...
...
tbl.RenderControl(w);
tableString = sw.ToString();

// Then my aspx page displays my tableString variable.

还有哪些其他方法我可以尝试手动将<thead>标记添加到我的循环中吗? 我正在使用System.Web.UI.WebControls。

1 个答案:

答案 0 :(得分:1)

您将TableRow控件设置为<thead>属性为<asp:TableRow TableSection="TableHeader"/>

例如:<thead>将在表格的rowControl.TableSection = TableRowSection.TableHeader;部分生成TableRow控件。

动态地,您还可以设置http://localhost/

三个选项是TableHeader,TableBody和TableFooter。