如何获取对 Datalist 的 OnItemBound 上的控件的引用?

发布于 2024-08-14 05:54:40 字数 2380 浏览 5 评论 0原文

我需要在控件上设置自定义属性,因为它绑定到数据列表。我看到事件参数有一个控件集合,但我没有看到与它们关联的任何引用名称。这怎么能做到呢?

当我尝试这样做时:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乙白 2024-08-21 05:54:40
(e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()");
(e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()");
别理我 2024-08-21 05:54:40

我正在后面的代码中构建我的 ItemTemplate (不是我的选择)。无论如何,您都必须明确命名您要查找的控件。

autoChartChkBox.ID = "autoChartChkBox";

然后,在 OnItemDataBound 事件中,您使用此 ID 作为 FindControl() 的参数。

就我而言:

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

希望这对其他人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文