我需要在控件上设置自定义属性,因为它绑定到数据专家。 我发现事件参数有一个控件集合,但我没有看到任何与它们相关的引用名称。 如何才能做到这一点?
当我尝试这个:
(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()");更多推荐
发布评论