如何在设计时初始化我的 Web 控件属性?
我需要一种方法来在将 Web 控件拖放到设计器区域时初始化其属性。作为“初始化”,我的意思是:如果我的控件有一个属性 Prop1。我需要为 Prop1 分配一个值,并且需要将该值保留在 ASPx 上。
我尝试了以下操作:
- 在我的 ControlDesigner 上实现 InitializeNewComponent:从未调用此方法。这似乎是一个错误。
- 在我的 ControlDesigner 上实现 Initialize:调用此方法,但不知何故 Visual Studio 不允许我在此阶段更改控件的属性。
- 创建 IComponentChangeService.ComponentAdded 的处理程序(应该在每次创建任何控件时调用该处理程序)。这个处理程序永远不会被调用。
我的问题:如何初始化我的控件属性?
PS:我知道如何设置控件的属性。我使用以下在智能标签上运行良好的代码:
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.Component)["Prop1"];
descriptor.SetValue("Value");
编辑
我忘了提及我无法使用 ToolBoxData,因为我需要进行处理以确定属性的值
I need a way to initialize my web control's properties when it's dropped on the designer area. As "initialize" I mean: If my control has a property Prop1. I need to assign a value to Prop1 and I need this value to be persisted on ASPx.
I tried the following:
- Implement InitializeNewComponent on my ControlDesigner: This method is never called. It seems a bug.
- Implement Initialize on my ControlDesigner: This method is called, but somehow Visual Studio does not allow me to change control's property at this stage.
- Create a handler to IComponentChangeService.ComponentAdded (that was supposed to be called every time any control is created). This handler is never called.
My question: How do I initialize my control properties?
PS: I know how to set my control's properties. I use the following code that works fine on a smart-tag:
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.Component)["Prop1"];
descriptor.SetValue("Value");
Edit
I forgot to mention I cannot use ToolBoxData because I need to make processing to determine the value of the property
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这样的方法。 MSDN
我也不太明白为什么要使用反射来设置已知类型的属性?
也许这可以帮助您一点:ASP.NET 控件开发速成课程:设计时特性和功能
There is no such method. MSDN
I also don't really understand why are you using reflection to set the property of a known type?
Maybe this can help you a bit: A Crash Course on ASP.NET Control Development: Design-Time Features and Capabilities