C# – How to pass method name to custom server control in asp.net

asp.netc++custom-server-controlsvb.netweb-controls

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

in vb.net: Parent.FindControl(TargetControlName)

I would like to pass a method to the control in the ASPX markup.

for example: <c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff">

So, I tried using reflection to access the given method name from the Parent.

Something like (in VB)

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")

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.


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

Best Solution

If you want to be able to pass a method in the ASPX markup, you need to use the Browsable attribute in your code on the event.

VB.NET

<Browsable(True)> Public Event InitializeStuffCallback

C#

[Browsable(true)]
public event EventHandler InitializeStuffCallback;

Reference: Design-Time Attributes for Components and BrowsableAttribute Class

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.