C# 如何获取对数据列表上控件的引用';这是什么?

C# 如何获取对数据列表上控件的引用';这是什么?,c#,.net,asp.net,datalist,C#,.net,Asp.net,Datalist,我需要在控件上设置自定义属性,因为它绑定到数据列表。我看到事件参数有一个控件集合,但我没有看到任何与它们相关联的引用名称。如何做到这一点 当我尝试这个: (e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken"); 控件始终为“null”。我试图查找的控件已添加到我的数据模板中。这是我的ItemTemplate作业,下面是实际的模板。注意protected复

我需要在控件上设置自定义属性,因为它绑定到数据列表。我看到事件参数有一个控件集合,但我没有看到任何与它们相关联的引用名称。如何做到这一点

当我尝试这个:

(e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken");
控件始终为“null”。我试图查找的控件已添加到我的数据模板中。这是我的
ItemTemplate
作业,下面是实际的模板。注意
protected复选框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;

            }

我正在代码隐藏中构建我的
ItemTemplate
(不是我的选择)。在任何情况下,都必须显式命名要查找的控件

autoChartChkBox.ID = "autoChartChkBox";
然后在
OnItemDataBound
事件中,使用此ID作为
FindControl()
的参数

就我而言:

protected void Data_ItemBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox control = (CheckBox)e.Item.FindControl("autoChartChkBox");
        }
    }
}

希望这对其他人有帮助。

这对我不起作用。控件始终为空。正在将控件添加到我的数据模板中。我已经更新了我的问题。你也可以发布你的aspx/ascx吗?以及一个合理的可复制集合(该类),您正试图用它填充数据列表?
protected void Data_ItemBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox control = (CheckBox)e.Item.FindControl("autoChartChkBox");
        }
    }
}