by Christoph Herold
21. Dezember 2006 11:34
While I was building an ASP.NET user control, I ran across the problem, how to serialize a property as the inner text of the Control in the ASPX-Code. I found a nice attribute named PersistenceModeAttribute
, that allows you to specify, how a property is supposed to be persisted. Setting it to InnerDefaultProperty
or EncodedInnerDefaultProperty
should do the trick, I thought.
But, as I had to find out, this is only part of what needs to be done. There are two more Attributes, that control the parsing of a control: ParseChildrenAttribute
and PersistChildrenAttribute
. When you use InnerDefaultProperty
to persist a property, you must add [ParseChildren(true, "")]
and [PersistChildren(false)]
to your control's class declaration. Otherwise things won't persist.
It took me a while to find this solution, and I found a nice explanation of things here: http://alvinzc.blogspot.com/2006/10/aspnet-basic-of-custom-server-control_25.html.
Alvin did a really nice job of explaining, what the attributes do. So if you want the details, just visit his blog.