Imagine the following scenario - we have Page1 which contains controls Control A and Control B.
Say Control A has a button, and on the click of this button we want Control B to react. But we want to do this in an abstract fashion, i.e. we can't have Control B knowing anything about Control A, and vice versa.
That way we can develop these controls in isolation, and drive them by unit-testing.
Now, I thought I had the solution, just want to know what you guys think of it.
On Control A's button click, I put a 'message' on the Session, i.e. Session["MESSAGES"] = "ControlA_Click".
In Page1, on the Page_LoadComplete(), I put a call to ProcessMessages, which looks like this:
List<Message> messages = SessionMessages.GetMessageList(Page);
foreach(Message m in messages)
{
//Get Controls
ControlA controlA = FindControl("controlA") as ControlA;
controlA .ProcessMessage(m);
ControlB controlB = FindControl("controlB") as ControlB;
controlB.ProcessMessage(m);
}
in ControlB's ProcessMessage() method, we can react to the messages that ControlB is interested in, like so:
if (m.MessageName == SessionMessages.C_MESSAGE_SEARCH)
{
this.Visible = true;
}
To me, this seems to work. It allows us to develop these controls completely separately from eachother, while still allowing for inter-control-communication at an abstract level.
The only thing I can think of that might bring this crashing down is perhaps the ASP.NET life-cycle in relation to Pages and User Controls. The way I figure it though is that ALL events should have been processed on the controls before Page_LoadComplete() is called on the owning Page.
Thoughts?