I have a composite control that adds a TextBox and a Label control to its Controls collection. When i try to set the Label's AssociatedControlID to the ClientID of the Textbox 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 little background. I got this main-composite control that dynamically adds a number of 'elements' to its control collection. One of these elements happen to be this 'MatrixTextBox' which is the control consisting of a TextBox and a Label.
I hold the Label and TextBox as protected class variables and init them in CreateChildControls:
ElementTextBox = new TextBox();
ElementTextBox.ID = "tb";
Controls.Add(ElementTextBox);
ElementLabel = new Label();
ElementLabel.ID = "lb";
Controls.Add(ElementLabel);
I tried setting the
ElementLabel.AssociatedControlID = ElementTextBox.ClientID;
both right after adding the controls to the Controls collection and even in PreRender – both yield the same error. What am i doing wrong?
Best Answer
I think you mustn't use the ClientID property of the ElementTextBox, but the ID. ClientID is the page-unique ID you'd have to use in Javascript, e.g. in the document.getElementyById and is not the same as the server-side ID - especially if you have a masterpage and/or controls in controls etc.
So it should be:
Hope this helps.