如何获取Datalist的OnItemBound控件的引用?(How do I get reference to a control on a Datalist's OnItemBound?

编程入门 行业动态 更新时间:2024-10-09 05:22:17
如何获取Datalist的OnItemBound控件的引用?(How do I get reference to a control on a Datalist's OnItemBound?)

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

当我尝试这个:

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

该控件始终为“空”。 我试图找到的控件已添加到我的数据模板中。 这是我的ItemTemplate作业,下面是实际的寺庙。 注意protected CheckBox autoChartChkBox; 这是我试图通过OnDataItemBound事件操作的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; }

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()"); (e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()");

更多推荐

本文发布于:2023-08-01 19:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   OnItemBound   Datalist   reference   control

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!