Labeling an AssociatedControlID Fails

I have a composite control that adds a TextBox and Label control to its Controls collection. When I try to set the Label AssociatedControlID to ClientID in a text box, I get this error

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

Ok, so a bit of background. I got this basic composite control that dynamically adds a few “elements” to my control collection. One of these elements is the "MatrixTextBox", which is a control consisting of a TextBox and a label.

I store Label and TextBox as protected class variables and run them in CreateChildControls:

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

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

I tried to install

ElementLabel.AssociatedControlID = ElementTextBox.ClientID;

Controls PreRender - . ?

+3
2

, ClientID ElementTextBox, ID. ClientID - , Javascript, . document.getElementyById , / ..

, :

ElementLabel.AssociatedControlID = ElementTextBox.ID;

, .

+7

, , :

, AssociatedControlID , . , , , .

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);
}
+3

Source: https://habr.com/ru/post/1698560/


All Articles