我需要在控件上设置自定义属性,因为它绑定到数据列表。我看到事件参数有一组控件,但我没有看到任何与它们关联的引用名称。如何才能做到这一点?
当我尝试这个时:
(e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken");
控件始终为“空”。我试图定位的控件已添加到我的数据模板中。这是我的ItemTemplate
任务,下面是真正的寺庙。请注意,protected CheckBox autoChartChkBox;
这是我试图通过OnDataItemBound
事件操作的控件。
alertList.ItemTemplate = new AlertItemTemplate(groupTracker);
private class AlertItemTemplate : ItemTemplateBase
{
private readonly GroupHeaderTracker groupTracker;
protected CheckBox autoChartChkBox;
public override void DataBind()
{
Label autoChartLbl;
Alert item = (Alert)((DataListItem)this.NamingContainer).DataItem;
CultureInfo info = Thread.CurrentThread.CurrentCulture;
titleText.Text = String.Format("{0} - {1}", item.DateCreated.ToString(info.DateTimeFormat.ShortDatePattern), item.ID);
this.bodyText.Text = item.Text;
Color color = GetAlertColor(item.AlertType.Color);
colorDisplay.BackColor = color;
this.groupTracker.SetCurrentAlertTypeId(item.AlertType.ID);
if(this.groupTracker.IsNewGroup())
{
this.alertTypeNameLabel.Text = item.AlertType.Name;
this.alertTypeNameRow.Visible = true;
this.alertTypeNameRow.Cells[0].Style.Add("border-top", string.Format("solid thin {0}",GetColorHexValue(color)));
this.alertTypeNameRow.Cells[0].Style.Add("border-bottom", string.Format("solid thin {0}",GetColorHexValue(color)));
//Auto Chart
TableCell autoChartCell;
autoChartCell = new TableCell();
autoChartCell.Width = 50;
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;
}