User Control Interaction
posted on 2005-09-13 at 21:20:17 by Joel Ross
I've been working on this for a while, but I was prodded to finish it after seeing Erik's post about the same subject. With my last project, one of the things we did early on was come up with a large set of reusable controls that could easily be used across many pages. This came in handy because each control handled how it displayed itself and the page didn't have to worry about each control's state - you know, the standard reasons for using controls.
But, what happens when you need something to happen in one user control based on something else happening in another user control? For example, we had a control where you enter your address, and how long you've lived there. Another control, which accepted a previous address, had to only be shown when the amount of time lived in your current address was less than 2 years. So how do you accomplish that interaction in a generic fashion?
Erik mentions an article in asp.netPRO magazine, which I don't have access to - I'm hoping we have a copy in the office (I've seen that magazine there before), so I can check it out, so just to be clear, I haven't read the article, and therefore I don't if my way is the best way, or if there is a better way. On the flip side, if my way is exactly what the article suggests, I am in no way trying to copy content - I haven't even read the article yet!
Anyway, here's what we did. In the particular example, the interaction was client side, so the PreviousAddress control would expose a javascript method that would be called to either show or hide itself - basically a get method that returned the name of the method to call. The CurrentAddress would have a property that you could set to tell it what javascript function to call when the amount of time is changed - and it's designed to pass in true if it's below 2 years, and false when it's above. The page then wires the two together - it tells the CurrentAddress control to call the method returned by the PreviousAddress control.
It may not be the best, but it worked out great for us, and works reliably and generically. We used this same pattern in multiple places, and never had any issues. Maybe I'll revisit this if I can get a hold of the magazine and see how the pros recommend this interaction.
Categories: ASP.NET