0

I am writing a composite control in c# asp.net as an experiment and would like to have all the instances of my composite control interact with one external control. Is this possible?

Things I have tried:

option 1: check from within the control whether an external control exists on the page, and add it if it doesn't

option 2: have the target control's id passed to the composite control at design time and then use this.Page.FindControl()

Obviously it was wishful thinking that it would be that simple :)

If I try do this from within the CreateChildControls Method, this.Page.FindControl(target control) always returns null. If I try to add the control to the page from within this method, it throws an exception:

"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."

is there another method / event where I can achieve this?

Evert
  • 8,161
  • 1
  • 15
  • 17
  • what do you mean by "have all the instances of my composite control interact with one external control"? Also, don't create dependencies between separate controls like this - access them from the page via control properties. – IrishChieftain Feb 26 '12 at 15:32
  • I basically want to be able to add a number of instances of my composite control to the page, and then have all of them output to the same panel, instead of adding a panel per composite control. – Evert Feb 26 '12 at 15:38
  • So you're using the composite control to contain other controls? Just curious if you actually need a composite one? What does your composite control do? – IrishChieftain Feb 26 '12 at 15:49
  • the composite control itself works as expected, and contains a few form controls. I just need to figure out how to create or reference another control on the page from within the code of the composite control, if it is at all possible. – Evert Feb 26 '12 at 16:23
  • Evert, I've updated my answer - basically, set a property on a control to make something in that control available publicly. – IrishChieftain Feb 26 '12 at 16:28

2 Answers2

0

You can create multiple instances on the same Web form by implementing the INamingContainer Interface. This basically helps prevent id clashes in the same namespace.

If you want to access another control set a property on it to expose the data you want made public.

Build Composite Controls

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
0

Why don't you expose a public property on your Composite Control of what output from them, then when rendering the Panel's contents, recurse through the page, find all instances of the composite control, grab the text, and add it to the panel?

TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43