在数据绑定期间添加复选框
我有一个数据列表,我试图为绑定到数据列表的每条记录插入一个复选框。第一条记录有复选框,但后续记录没有。我怀疑我所做的只是在每次绑定记录时替换第一个复选框。有人能给我一些见解吗?我需要为每条记录重复该复选框。
alertList.ItemTemplate = new AlertItemTemplate(groupTracker);
if (!Page.IsPostBack) {
alertList.DataBind();
}
这是我重写的绑定方法:
public override void DataBind()
{
//Auto Chart
TableCell autoChartCell;
autoChartCell = new TableCell();
autoChartCell.BorderStyle = BorderStyle.Solid;
autoChartCell.VerticalAlign = VerticalAlign.Top;
autoChartCell.Controls.Add(autoChartChkBox = new CheckBox());
autoChartCell.Controls.Add(autoChartLbl = new Label());
Rows[1].Cells.Add(autoChartCell);
autoChartLbl.Text = "AutoChart";
autoChartChkBox.Checked = item.IncludeInChartNotes;
alertTypeNameCell.ColumnSpan = Rows[1].Cells.Count;
}
}
I have a Datalist and I am trying to insert a checkbox for each record that gets bound to the datalist. The first record has the checkbox but the subsequent records do not. I suspect that what I am doing is just replacing the first checkbox eachtime a record is bound. Can someone give me some insight? I need the checkbox to be repeated for each record.
alertList.ItemTemplate = new AlertItemTemplate(groupTracker);
if (!Page.IsPostBack) {
alertList.DataBind();
}
Here is my overridden bind method:
public override void DataBind()
{
//Auto Chart
TableCell autoChartCell;
autoChartCell = new TableCell();
autoChartCell.BorderStyle = BorderStyle.Solid;
autoChartCell.VerticalAlign = VerticalAlign.Top;
autoChartCell.Controls.Add(autoChartChkBox = new CheckBox());
autoChartCell.Controls.Add(autoChartLbl = new Label());
Rows[1].Cells.Add(autoChartCell);
autoChartLbl.Text = "AutoChart";
autoChartChkBox.Checked = item.IncludeInChartNotes;
alertTypeNameCell.ColumnSpan = Rows[1].Cells.Count;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在视图标记中添加所需的控件,然后根据需要以编程方式显示/隐藏。
通常不鼓励将此类代码放在代码隐藏中,因为它会破坏控制器/视图模型。
Add the required controls in your view markup, then show/hide programmatically as necessary.
Putting this kind of code in code-behinds is generally discouraged as it breaks the controller/view model.