Thursday, October 13, 2005

Trigger a page method from a User Control

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/trigger_a_page_method_from_a_user_control.htm]

[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:

The last two links are from MSDN, and therefore have code samples in both VB.Net and C#.

No comments:

Post a Comment