{"id":318,"date":"2019-02-05T11:50:34","date_gmt":"2019-02-05T15:50:34","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=318"},"modified":"2019-02-06T23:13:17","modified_gmt":"2019-02-07T03:13:17","slug":"raising-handling-custom-usercontrol-events-in-asp-net","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/raising-handling-custom-usercontrol-events-in-asp-net\/","title":{"rendered":"Raising &#038; Handling Custom UserControl Events in ASP.Net"},"content":{"rendered":"<p>Declare the event in the UserControl and create a method that raises it. The event is named \"SelectionChanged\".<\/p>\n<p><strong>UserControl.ascx<\/strong><\/p>\n<pre><code>\/\/\/ &lt;summary&gt;\n\/\/\/ Event to be raised by control when the selections change\n\/\/\/ &lt;\/summary&gt;\npublic event EventHandler SelectionChanged;\n\nprotected virtual void OnSelectionChanged(EventArgs e) {\n    SelectionChanged?.Invoke(this, e);\n}\n<\/code><\/pre>\n<p>Add an event handler for the event in the page which contains the UserControl. In this case, the ID of the UserControl is \"Toolbar\".<\/p>\n<p><strong>Page.aspx<\/strong><\/p>\n<pre><code>protected void Page_Load(object sender, EventArgs e) {\n    Toolbar.SelectionChanged += new EventHandler(Toolbar_SelectionChanged);\n}\n\nprivate void Toolbar_SelectionChanged(object sender, EventArgs e) {\n    \/\/ Event raised from page - do stuff here\n}\n<\/code><\/pre>\n<p>For VB.Net examples, see various projects (e.g. WPHO)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Declare the event in the UserControl and create a method that raises it. The event is named &#8220;SelectionChanged&#8221;. UserControl.ascx \/\/\/ &lt;summary&gt; \/\/\/ Event to be raised by control when the  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[9,12],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-asp-net","tag-asp-net","tag-c"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-58","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":7,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/318\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}