I am working on a Customer Server Control that extends another control. There is no problem with attaching to other controls on the form.
我正在开发一个扩展另一个控件的Customer Server Control。附加到表单上的其他控件没有问题。
in vb.net: Parent.FindControl(TargetControlName)
在vb.net中:Parent.FindControl(TargetControlName)
I would like to pass a method to the control in the ASPX markup.
我想将一个方法传递给ASPX标记中的控件。
for example: <c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff">
例如:
So, I tried using reflection to access the given method name from the Parent.
所以,我尝试使用反射从Parent访问给定的方法名称。
Something like (in VB)
像(在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.
找不到该方法,因为它不是Page的一部分。我应该在哪里看?我很确定这是可能的,因为我已经看到其他控件类似。
I forgot to mention, my work-around is to give the control events and attaching to them in the Code-behind.
我忘了提及,我的解决方法是给控制事件并在Code-behind中附加它们。
5 个解决方案
#1
2
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.
如果您希望能够在ASPX标记中传递方法,则需要在事件的代码中使用Browsable属性。
VB.NET
<Browsable(True)> Public Event InitializeStuffCallback
C#
[Browsable(true)]
public event EventHandler InitializeStuffCallback;
Reference: Design-Time Attributes for Components and BrowsableAttribute Class
参考:组件和BrowsableAttribute类的设计时属性
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.
所有事件,属性或其他任何需要在控件的代码隐藏中使用browsable属性来实现它,以便您可以在标记代码中更改它。
#2
2
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#)
通常,您不需要通过反射获取方法。在你的用户控件里面,定义一个公共事件(抱歉,我不知道vb语法所以这将在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:
现在,在您的aspx页面或用户控件的任何容器内,定义一个与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">
#3
0
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.
您的解决方法实际上是更好的答案。如果您的代码必须在控件生命周期的某个部分运行,那么您应该公开事件以使容器使用自定义功能延长生命周期。
#4
0
buyutec and Jesse Dearing both have an acceptable answer.
buyutec和Jesse Dearing都有一个可以接受的答案。
[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.
我之前忽略的事实是,当您从标签引用控件时,它会准备结束。
#5
0
Every ASP.NET page is class of its own inherited from Page
as in:
每个ASP.NET页面都是自己继承自Page的类,如下所示:
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.
因此,要通过Reflection查找该方法,必须获取正确的类型,即存储页面代码的页面类的类型。
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.
我想你需要支持多个页面才能实例化这个控件我相信你可以通过Reflection找到Page的任何实例的子类型,但是我不记得怎么做,但你应该能够做到。
but... like everyone else has said, such case is what events are for.
但......就像其他人所说的那样,这种情况就是事件的发生。
#1
2
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.
如果您希望能够在ASPX标记中传递方法,则需要在事件的代码中使用Browsable属性。
VB.NET
<Browsable(True)> Public Event InitializeStuffCallback
C#
[Browsable(true)]
public event EventHandler InitializeStuffCallback;
Reference: Design-Time Attributes for Components and BrowsableAttribute Class
参考:组件和BrowsableAttribute类的设计时属性
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.
所有事件,属性或其他任何需要在控件的代码隐藏中使用browsable属性来实现它,以便您可以在标记代码中更改它。
#2
2
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#)
通常,您不需要通过反射获取方法。在你的用户控件里面,定义一个公共事件(抱歉,我不知道vb语法所以这将在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:
现在,在您的aspx页面或用户控件的任何容器内,定义一个与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">
#3
0
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.
您的解决方法实际上是更好的答案。如果您的代码必须在控件生命周期的某个部分运行,那么您应该公开事件以使容器使用自定义功能延长生命周期。
#4
0
buyutec and Jesse Dearing both have an acceptable answer.
buyutec和Jesse Dearing都有一个可以接受的答案。
[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.
我之前忽略的事实是,当您从标签引用控件时,它会准备结束。
#5
0
Every ASP.NET page is class of its own inherited from Page
as in:
每个ASP.NET页面都是自己继承自Page的类,如下所示:
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.
因此,要通过Reflection查找该方法,必须获取正确的类型,即存储页面代码的页面类的类型。
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.
我想你需要支持多个页面才能实例化这个控件我相信你可以通过Reflection找到Page的任何实例的子类型,但是我不记得怎么做,但你应该能够做到。
but... like everyone else has said, such case is what events are for.
但......就像其他人所说的那样,这种情况就是事件的发生。