开发者

How do I get reference to a control on a Datalist's OnItemBound?

开发者 https://www.devze.com 2022-12-13 15:13 出处:网络
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

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;

            }


(e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消