【发布时间】:2014-09-02 00:57:41
【问题描述】:
我有一个gridview,它在gridview 的headerTemplate 中动态填充复选框。标题是一个复选框和一个动态文本/标签。但是我无法让 headertemplate 显示文本:
页面加载:
foreach (Module m in listModule)
{
TemplateField tfield = new TemplateField();
CheckBox cbAll = new CheckBox();
tfield.HeaderTemplate = new TickColumn();
tfield.HeaderText = m.ModuleName; //<- This is not getting displayed.
gvUsers.Columns.Add(tfield);
}
class TickColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
CheckBox cbAll = new CheckBox();
cbAll.ID = "cbAll";
container.Controls.Add(cbAll);
}
}
由于某种原因,没有显示 HeaderText。
或者 我尝试在类中添加一个标签,但我不知道如何将值传递给:
static string lblname;
页面加载:
foreach (Module m in listModule)
{
TemplateField tfield = new TemplateField();
CheckBox cbAll = new CheckBox();
tfield.HeaderTemplate = new TickColumn();
lblname = m.ModuleName;
gvUsers.Columns.Add(tfield);
}
class TickColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
CheckBox cbAll = new CheckBox();
Label lbl = new Label();
lbl.Text = lblName;
cbAll.ID = "cbAll";
container.Controls.Add(cbAll);
container.Controls.Add(lbl);
}
}
使用此代码,每个动态列标签的文本变得相同,最后一个元素名称显示在所有标签中。我该如何解决这个问题?
【问题讨论】: