[UPDATE 11/20/2006] - New post with code sample
User controls let you group UI-functionality together into a resuable control. For example, headers, footers, menus, and groups of controls like userId-passwords and addresses all make good user controls. Effective components need to both pass data in (i.e. be able to have the page set the values of the user control), and pass data out (be able to have the page retrieve the values from fields in the UserControl). Besides just passing data, we also want to be able to trigger events. For example you could click a button on a UserControl or Page, and that triggers a method from the UC. But how do you do it the other way - click a button on the user control and have it trigger a method on the page. There are several ways.
- One way of using delegates, as mentioned in this article I wrote back in early 2004 for 4GuysFromRolla.
However, I have since seen better ways, and I would suggest either:
- Use events and delegates, and add an event to the user control. This applies to all .Net components, not just WebForms.
- Use the RaiseBubbleEvent.
The last two links are from MSDN, and therefore have code samples in both VB.Net and C#.