Hi Hugh,
I think the best way to set this up is with event handlers on the controls.
1. In each control, define a delegate like this:
Code:
public delegate void ChangeEventHandler(object sender, ChangeEventArgs e);
2. In each control, define a public event for the parent page to subscribe to:
Code:
public event ChangeEventHandler Change;
3. Define a method that triggers the event from within your code:
Code:
protected virtual void OnChange(ChangeEventArgs e)
{
if(Change != null)
{
Change(this, e);
}
}
4. Call this method from anywhere in your code where it's appropriate; e.g. in the Button_Click or Selected_Index changed of your grid, for example:
Code:
// Set up the EventArgs
Code:
ChangeEventArgs myNewEventsArgs = new ChangeEventArgs();
myNewEventsArgs.SomeProperty = SomeValue;
// Raise Event
OnChange(myNewEventsArgs);
5. In this example, I used a custom class ChangeEventArgs. This is a simple class that inherits from System.EventArgs and has an additional property called SomeProperty.
If you don't need to pass additional info to the subscribers of the event, you can also use a normal System.EventArgs object as the parameter for your methods.
6. Next, in the parent page, set up the event for your control:
Code:
this.MyControl1.Change += new MyControl.ChangeEventHandler(this.MyControl1_OnChange);
7. And finally, set up the method to do something useful, like call a method on the other control:
Code:
private void MyControl1_OnChange(Object sender, ChangeEventArgs e)
{
if(e.SomeProperty == SomeValue)
{
MyControl2.SomeMethod();
}
else
{
SomeOtherMethod();
}
}
I think this is the cleanest way to do it, as there is no coupling between Control1 and Control2, and only a loosly coupling between the Controls and the parent page, which is fine.
IMO, this is better than using FindControl, because there is no coupling and you also no longer depend on the (hardcoded) names of Control1 and Control2.
Out of curiosity: didn't my FindControl solution work? I did a quick test with two controls and I managed to make this work. What went wrong?
If you want to find out more about controls and how they work, take a look at the book
Server Controls and Components. It's not an easy book to read, but it provides valuable insight in how (custom / server / user) controls work.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.