{"id":5831,"date":"2014-04-07T09:34:02","date_gmt":"2014-04-07T09:34:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/using-createdelegate-with-different-signatures-collection-of-common-programming-errors\/"},"modified":"2014-04-07T09:34:02","modified_gmt":"2014-04-07T09:34:02","slug":"using-createdelegate-with-different-signatures-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/using-createdelegate-with-different-signatures-collection-of-common-programming-errors\/","title":{"rendered":"Using CreateDelegate with different signatures-Collection of common programming errors"},"content":{"rendered":"<p>I am writing a custom control that is an extender for an ASP.Net Control.\u00a0 My extender needs to add an EventHandler to the Control to perform some action when an event occurs.\u00a0 In most cases, the control will be a button and the event will be Click.\u00a0 However, the extender will\u00a0not always\u00a0be used on a button.\u00a0 The user gives the control ID and EventName at design time.<\/p>\n<p>At runtime, my extender gets a reference to the Control and the Event.\u00a0 Then it adds a handler to that event with the following code:<\/p>\n<p>\/\/ &#8211; Init<\/p>\n<p>Delegate d = Delegate.CreateDelegate(TheEvent.EventType,this,&#8221;ClickHandler&#8221;);<\/p>\n<p>TheEvent.AddEventHandler(TheButton, d);<\/p>\n<p>\/\/<\/p>\n<p>void ClickHandler(object sender, EventArgs e)<\/p>\n<p>{<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/some logic<\/p>\n<p>}<\/p>\n<p>This works for a button\u2019s Click event and for a drop down\u2019s SelectedItemChanged event. \u00a0\u00a0However, it will not work for the Click event of an ImageButton.\u00a0 The signature of the click event for the image button is different.\u00a0 The second parameter is an ImageClickEventArgs instead of an EventArgs.<\/p>\n<p>The ImageClickEventArgs class inherits from EventArgs but I still get the following runtime error.<\/p>\n<p>\u201cError binding to target method.\u201d<\/p>\n<p>\u00a0At design time I do not know all the possible controls and events that this extender may be used with.\u00a0 I just need to intercept the event and perform a task.\u00a0 I do not need the EventArgs parameter for my task.<\/p>\n<p>Any suggestions on how to get this working?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am writing a custom control that is an extender for an ASP.Net Control.\u00a0 My extender needs to add an EventHandler to the Control to perform some action when an event occurs.\u00a0 In most cases, the control will be a button and the event will be Click.\u00a0 However, the extender will\u00a0not always\u00a0be used on a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5831","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5831"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5831\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}