如何获取对 Datalist 的 OnItemBound 上的控件的引用?
我需要在控件上设置自定义属性,因为它绑定到数据列表。我看到事件参数有一个控件集合,但我没有看到与它们关联的任何引用名称。这怎么能做到呢?
当我尝试这样做时:
(e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken");
控件始终为“空”。我尝试查找的控件已添加到我的数据模板中。这是我的 ItemTemplate
作业,下面是实际的寺庙。注意 protected CheckBox 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在后面的代码中构建我的
ItemTemplate
(不是我的选择)。无论如何,您都必须明确命名您要查找的控件。然后,在
OnItemDataBound
事件中,您使用此 ID 作为FindControl()
的参数。就我而言:
希望这对其他人有帮助。
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.Then in the
OnItemDataBound
event you use this ID as the argument toFindControl()
.In my case:
Hope this helps someone else.