简体   繁体   English

如何在设计时初始化我的Web控件属性?

[英]How to initialize my web control properties at design-time?

I need a way to initialize my web control's properties when it's dropped on the designer area. 当将Web控件放置在设计器区域时,我需要一种初始化其属性的方法。 As "initialize" I mean: If my control has a property Prop1. 作为“初始化”,我的意思是:如果我的控件具有属性Prop1。 I need to assign a value to Prop1 and I need this value to be persisted on ASPx. 我需要为Prop1分配一个值,并且需要将该值保留在ASPx上。

I tried the following: 我尝试了以下方法:

  1. Implement InitializeNewComponent on my ControlDesigner: This method is never called. 在ControlDesigner上实现InitializeNewComponent:永远不会调用此方法。 It seems a bug. 似乎是个错误。
  2. 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. 在ControlDesigner上实现Initialize:调用此方法,但是在某种程度上,Visual Studio不允许我在此阶段更改控件的属性。
  3. Create a handler to IComponentChangeService.ComponentAdded (that was supposed to be called every time any control is created). 创建一个IComponentChangeService.ComponentAdded处理程序(应该在每次创建任何控件时调用该处理程序)。 This handler is never called. 永远不会调用此处理程序。

My question: How do I initialize my control properties? 我的问题:如何初始化控件属性?

PS: I know how to set my control's properties. PS:我知道如何设置控件的属性。 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 我忘了提到我不能使用ToolBoxData,因为我需要进行处理以确定该属性的值

[ToolboxData("<{0}:MyControl runat=\"server\" Prop1=\"Value\" />")]
public class MyControl
{
}
descriptor.SetValue("Value");

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? MSDN我也不太了解您为什么要使用反射来设置已知类型的属性?

Maybe this can help you a bit: A Crash Course on ASP.NET Control Development: Design-Time Features and Capabilities 也许这可以对您有所帮助: ASP.NET控件开发速成班:设计时功能和功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM