<p>I am working on a Customer Server Control that extends another control.  There is no problem with attaching to other controls on the form.</p>

<p>in vb.net:  <code>Parent.FindControl(TargetControlName)</code></p>

<p>I would like to pass a method to the control in the ASPX markup. </p>

<p>for example: <code>&lt;c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff"&gt;</code></p>

<p>So, I tried using reflection to access the given method name from the Parent.</p>

<p>Something like (in VB)</p>

<pre class="lang-vb prettyprint-override"><code>Dim pageType As Type = Page.GetType
Dim CallbackMethodInfo As MethodInfo = pageType.GetMethod( "MethodName" )

'Also tried 
sender.Parent.GetType.GetMethod("MethodName")
sender.Parent.Parent.GetType.GetMethod("MethodName")
</code></pre>

<p>The method isn't found, because it just isn't apart of the Page.  Where should I be looking?  I'm fairly sure this is possible because I've seen other controls do similar.</p>

<hr>

<p>I forgot to mention, my work-around is to give the control events and attaching to them in the Code-behind.</p>

[{'answer_id': 33163, 'author': 'Joel Martinez', 'author_id': 3433, 'author_profile': 'https://Stackoverflow.com/users/3433', 'pm_score': 0, 'selected': False, 'text': "<p>Your workaround is actually the better answer.  If you have code that you must run at a certain part of your control's lifecycle, you <strong>should</strong> expose events to let the container extend the lifecycle with custom functionality.</p>\n"}, {'answer_id': 33179, 'author': 'Jesse Dearing', 'author_id': 1804, 'author_profile': 'https://Stackoverflow.com/users/1804', 'pm_score': 3, 'selected': True, 'text': '<p>If you want to be able to pass a method in the ASPX markup, you need to use the <code>Browsable</code> attribute in your code on the event.</p>\n\n<p>VB.NET</p>\n\n<pre class="lang-vb prettyprint-override"><code>&lt;Browsable(True)&gt; Public Event InitializeStuffCallback\n</code></pre>\n\n<p>C#</p>\n\n<pre><code>[Browsable(true)]\npublic event EventHandler InitializeStuffCallback;\n</code></pre>\n\n<p>Reference:\n<a href="http://msdn.microsoft.com/en-us/library/tk67c2t8.aspx" rel="nofollow noreferrer">Design-Time Attributes for Components</a> and <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx" rel="nofollow noreferrer">BrowsableAttribute Class</a></p>\n\n<p>All the events, properties, or whatever need to be in the code-behind of the control with the browsable attribute to make it so you can change it in the tag code.</p>\n'}, {'answer_id': 33187, 'author': 'Serhat Ozgel', 'author_id': 31505, 'author_profile': 'https://Stackoverflow.com/users/31505', 'pm_score': 2, 'selected': False, 'text': '<p>Normally you wouldn\'t need to get the method via reflection. Inside your user control, define a public event (sorry I do not know the vb syntax so this will be in c#)</p>\n\n<pre><code>public event EventHandler EventName;\n</code></pre>\n\n<p>Now, inside your aspx page, or whatever container of the user control, define a protected method that matches the EventHandler:</p>\n\n<pre><code>protected void MyCustomerControl_MethodName(object sender, EventArgs e) { }\n</code></pre>\n\n<p>Now, inside your markup, you can use</p>\n\n<pre><code>&lt;c:MyCustomerControl id="MyCustomerControl" runat=server OnEventName="MyCustomerControl_MethodName"&gt;\n</code></pre>\n'}, {'answer_id': 33191, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': '<p>Every ASP.NET page is <strong>class of its own</strong> inherited from <code>Page</code> as in:</p>\n\n<pre><code>class MyPage : Page\n</code></pre>\n\n<p>Therefore, to find that method via Reflection, you must get the correct type, which is the type of the page class that stores the page code.</p>\n\n<p>I suppose you need to support multiple pages for this control to be instantiated in I believe you can find the child type of any instance of Page via Reflection, but I do not remember how, but you should be able to do it.</p>\n\n<p>but... like everyone else has said, such case is what <em>events</em> are for.</p>\n'}, {'answer_id': 33536, 'author': 'oglester', 'author_id': 2017, 'author_profile': 'https://Stackoverflow.com/users/2017', 'pm_score': 0, 'selected': False, 'text': "<p>buyutec and Jesse Dearing both have an acceptable answer.</p>\n\n<pre><code>[Browsable(true)]  \n</code></pre>\n\n<p>lets you see the property in the Properties window.  However, the event doesn't show up, which makes no difference to me.</p>\n\n<p>The thing I overlooked earlier was the fact that when you reference a control's even from the tag, it prep-ends On.</p>\n"}]
['https://Stackoverflow.com/questions/33150', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2017/']