views:

402

answers:

3

I have created a UserControl that has a ListView in it. The ListView is publicly accessible though a property. When I put the UserControl in a form and try to design the ListView though the property, the ListView stays that way until I compile again and it reverts back to the default state.

How do I get my design changes to stick for the ListView?

A: 

Just so I'm clear, you've done something like this, right?

public ListView MyListView { get { return this.listView1; } }

So then you are accessing (at design time) the MyListView property on your UserControl?

I think if you want proper design-time support you're better off changing the "Modifier" property on the ListView itself (back on the original UserControl) to Public - that way you can modify the ListView directly on instances of the UserControl. I've had success doing that anyway.

Matt Hamilton
+4  A: 

You need to decorate the ListView property with the DesignerSerializationVisibility attribute, like so:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ListView MyListView { get { return this.listView1; } }

This tells the designer's code generator to output code for it.

Fredrik Kalseth
A: 

Fredrik is right, basically, when you need to enable the designer to persist the property to page so it can be instantiated at run time. There is only one way to do this, and that is to write its values to the ASPX page, which is then picked up by the runtime.

Otherwise, the control will simply revert to its default state each and every time.

Always keep in the back of your mind that the Page (and its contents) and the code are completely seperate in ASP.NET, they are hooked up at run time. This means that you dont get the nice code-behind designer support like you do in a WinForms app (where the form is an instance of an object).

Rob Cooper