1

I have 5 user controls on the page and each control implements it's own interface that contains properties and events. In order to enable communication between user controls, I am creating a property of target user control inside other user control. Through this property, it's state can be altered and be able to register its events.

Below is the pseudo code of results user control. It subscribes to OnSearch event of Search user control.

public interface IResults
{
     //other fields
    ISearch SearchControl { get;}
}

public partial class Results : IResults
{
 //other fields

 public ISearch SearchControl
 {

    get{
    this.Page.Parent.FindControl("UCSearch") as ISearch;}
 }

 protected override void Page_Load(object sender, EventArgs e)
 {
    this.SearchControl.OnSearch += new EventHandler(testMethod);
 }
}

Is it ok to have the reference properties inside user controls to subscribe to the events and manipulating the state. Does it create any maintenance problem in future.

Does FindControl and type casting degrade the performance of the application.

  • I feel like what you're seeing here is a dependency, which might make it a good idea to inject it as a property. I'm not sure if the dependency is even the search control or just some form of communication, but I'd definitely try to avoid letting the UserControls access each other this way. This would be a simple [Import] with MEF, but that would have far larger complications. – Magus Apr 18 '14 at 22:31
  • Yes, it's a dependency but it is tied with user controls. I never configured .ascx.cs to the interface through dependency injection. – Sunny Apr 18 '14 at 22:34
  • It doesn't matter how you inject something, just that you DO inject something. You could simply pass it in from whatever context adds the UserControls. I'd probably make some kind of communication class for it, though. – Magus Apr 18 '14 at 22:36
  • You mean, in the main page load event, assign the SearchControl property of the results control. This way all dependencies are resolved from main page. Is my understanding correct. – Sunny Apr 18 '14 at 23:01
  • That sounds about right, if the main page load event is where your Results class is constructed. The idea is, you make they objects and give them what they need before letting them use it. That's what Dependency Injection is. Now, I might try to abstract it so you don't directly reference SearchControl, but if you want to do that, you can plan something to implement it. – Magus Apr 18 '14 at 23:06
3

In general you might want to establish in your architecture, a rule of thumb that controls in your design will not know about other control(s). If they do, you can end up with spaghetti and cross-referencing difficulties. Consider introducing a Mediator, where only the Mediator knows about all the controls. If a control wants to talk to another control, it can do so only through the Mediator. The control raises an event, which is fielded by the Mediator. The Mediator has knowledge of what that event means to some other control on the page. It calls that control to do the work. The control that raised the event does not even know the other control exists.

  • Thank you for your response. I am facing other issue. I need to pass the search criteria from one control to another control where grid is present. So, I have created a custom event args and passed it. But my issue is when the user clicks on grid column to sort, it needs to get search criteria – Sunny Apr 21 '14 at 21:04
  • Since application doesn't maintain state I am confused whether I need to put in some sort of cache when user clicks on the button and not pass any arguments or do I need implement the caching where I need it. – Sunny Apr 21 '14 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.