gpt4 book ai didi

c# - 如何将方法名称传递给 asp.net 中的自定义服务器控件?

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:39 24 4
gpt4 key购买 nike

我正在开发一个扩展另一个控件的客户服务器控件。附加到窗体上的其他控件没有问题。

在 vb.net 中:Parent.FindControl(TargetControlName)

我想将方法​​传递给 ASPX 标记中的控件。

例如:<c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff">

因此,我尝试使用反射从 Parent 访问给定的方法名称。

类似(在 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")

未找到该方法,因为它不属于页面。我应该在哪里看?我相当确定这是可能的,因为我看到其他控件也有类似的功能。


我忘了说,我的解决方法是在代码隐藏中提供控制事件并附加到它们。

最佳答案

如果您希望能够在 ASPX 标记中传递方法,则需要在事件代码中使用 Browsable 属性。

VB.NET

<Browsable(True)> Public Event InitializeStuffCallback

C#

[Browsable(true)]
public event EventHandler InitializeStuffCallback;

引用: Design-Time Attributes for ComponentsBrowsableAttribute Class

所有事件、属性或任何需要包含在具有可浏览属性的控件的代码隐藏中,以便您可以在标记代码中更改它。

关于c# - 如何将方法名称传递给 asp.net 中的自定义服务器控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com