Saturday, August 1, 2009

Managing UserControls using Interfaces

It's a pretty common situation where you have multiple UserControls in a page which all expose the same functionalities.

In the project I'm working on right now, we have a big TabContainer. And each Tab is a UserControl. One button should make all the Tabs save their data, but not every Tab is always loaded into the TabContainer. As you can guess, we needed something generic.

That's why I introduced the ITab interface. Every UserControl implements this interface.

When I hit that save-button, a method iterates trough the ControlCollection and looks for every type that implements the ITab interface.

Take a look at this codesnippet.

The public method Iterates through the ControlCollection and checks if the Type is a Tab using the private IsTab() method. This private method uses Type.GetInterfaces() to iterate through all the Interfaces for that Type. If the control is a Tab, I cast it to an ITab and call a public method depending on the ReportMode.

No comments:

Post a Comment