代码之家  ›  专栏  ›  技术社区  ›  Per Hornshøj-Schierbeck

在标签上设置AssociatedControlID失败

  •  3
  • Per Hornshøj-Schierbeck  · 技术社区  · 16 年前

    我有一个复合控件,它将文本框和标签控件添加到控件集合中。当我试图将标签的AssociatedControlID设置为文本框的ClientID时,我得到了这个错误

    Unable to find control with id 
    'ctl00_MainContentPlaceholder_MatrixSetControl_mec50_tb'
    that is associated with the Label 'lb'. 
    

    好的,有点背景。我得到了这个主复合控件,它动态地向控件集合添加了许多“元素”。其中一个元素恰好是“MatrixtBox”,它是由一个文本框和一个标签组成的控件。

        ElementTextBox = new TextBox();
        ElementTextBox.ID = "tb";
        Controls.Add(ElementTextBox);
    
        ElementLabel = new Label();
        ElementLabel.ID = "lb";
        Controls.Add(ElementLabel);
    

    ElementLabel.AssociatedControlID = ElementTextBox.ClientID;
    

    将控件添加到控件集合之后,甚至在PreRender中,都会产生相同的错误。我做错了什么?

    2 回复  |  直到 16 年前
        1
  •  7
  •   splattne    16 年前

    不得使用ClientID 属性,但 身份证件

    因此,它应该是:

    ElementLabel.AssociatedControlID = ElementTextBox.ID;
    

    希望这有帮助。

        2
  •  3
  •   Patrick de Kleijn    16 年前

    可能对遇到错误的其他读者有帮助:

    private void AddRadioButton(PlaceHolder placeholder, string groupname, string text)
    {
        RadioButton radio = new RadioButton();
        radio.GroupName = groupname;
        radio.ID = Guid.NewGuid().ToString(); // Always set an ID.
    
        Label label = new Label();
        label.Text = text;
        label.AssociatedControlID = radio.ID;
    
        placeholder.Controls.Add(radio);
        placeholder.Controls.Add(label);
    }