views:

240

answers:

2

Hey,

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:

  1. Implement InitializeNewComponent on my ControlDesigner: This method is never called. 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.
  3. 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

+1  A: 
[ToolboxData("<{0}:MyControl runat=\"server\" Prop1=\"Value\" />")]
public class MyControl
{
}
Mehdi Golchin
Oh nooo.. Your answer perfectly solves the problem I described but not my problem. I forgot to mention this solution isn't valid because I need to make processing to determine the value of the property. It's not static. Thanks and upvoted anyway.
Ciwee
A: 
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?

Maybe this can help you a bit: A Crash Course on ASP.NET Control Development: Design-Time Features and Capabilities

kubal5003
1) Yes, this method exists. 2) I'm not using reflection I'm using ComponentModel which is required so that Visual Studio may update the ASPx and the PropertyGrid.
Ciwee