views:

88

answers:

2

Hey everyone,

I need to set custom attributes on a control as it is bound to a datalist. I see that the event arguments has a collection of controls but I do not see any reference name associated with them. How can this be done?

When I try this:

(e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken");

The control is always 'null'. The control I am trying to locate is added in my data template. This is my ItemTemplate assignment and below is the actual temple. Notice the protected CheckBox autoChartChkBox; This is the control I am trying to manipulate via the OnDataItemBound event.

   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;

            }

Can someone save me? :)

Thanks!

-Nick

+1  A: 
(e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()");
ram
This doesn't work for me. The control is always null. The control is being added in my data template. I have updated my question.
Nick
can you post your aspx/ascx as well ? and a reasonably reproducible collection (of that class) with which you are trying to populate the datalist ?
ram
A: 

I am building my ItemTemplate in the code behind (not my choice). In any case you have to explicitly name the control that you are trying to find.

autoChartChkBox.ID = "autoChartChkBox";

Then in the OnItemDataBound event you use this ID as the argument to FindControl().

In my case:

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");

} } }

Hope this helps someone else.

Nick