oglester
oglester

Reputation: 6655

How to pass method name to custom server control in asp.net?

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.

Upvotes: 1

Views: 2476

Answers (5)

chakrit
chakrit

Reputation: 61518

Every ASP.NET page is class of its own inherited from Page as in:

class MyPage : Page

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.

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.

but... like everyone else has said, such case is what events are for.

Upvotes: 0

Jesse Dearing
Jesse Dearing

Reputation: 2271

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.

Upvotes: 2

oglester
oglester

Reputation: 6655

buyutec and Jesse Dearing both have an acceptable answer.

[Browsable(true)]  

lets you see the property in the Properties window. However, the event doesn't show up, which makes no difference to me.

The thing I overlooked earlier was the fact that when you reference a control's even from the tag, it prep-ends On.

Upvotes: 0

Serhat Ozgel
Serhat Ozgel

Reputation: 23766

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

public event EventHandler EventName;

Now, inside your aspx page, or whatever container of the user control, define a protected method that matches the EventHandler:

protected void MyCustomerControl_MethodName(object sender, EventArgs e) { }

Now, inside your markup, you can use

<c:MyCustomerControl id="MyCustomerControl" runat=server OnEventName="MyCustomerControl_MethodName">

Upvotes: 2

Joel Martinez
Joel Martinez

Reputation: 1083

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 should expose events to let the container extend the lifecycle with custom functionality.

Upvotes: 0

Related Questions