Please: ITemplate propery being saved incorrectly -- Attributes usage incorrect?

S

S.Sigal

HELP!

Have a problem to do with Attributes and serialization: currently it's too greedy and is serializing
to the Html/XML even blank/null ITemplates properties -- rather than not serializing nothing
when the ITemplate == null.

Let me explain:

//SCENARIO 1 -- WORKS
First let's look at a scenario that works: I have an Itemplate that I want to expose as a property
of a webcontrol:
[
System.ComponentModel.DefaultValue(null),
System.ComponentModel.NotifyParentProperty(true),
System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty),
System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public ITemplate TestTemplate {get {return _TestTemplate;}set {_TestTemplate = value;}}
private ITemplate _TestTemplate = null;

Perfect.
I muck around in the IDE, change other properties, and the IDE forces a save to the
XML/HTML of the control.

The result looks like:

<CC:MYCONTROL BackColor="red"/>

No referance to TestTemplate because TestTemplate is null.
Good -- that's what we want.



//SCENARIO 2 -- DOESNT WORK:
Now -- Let's look at what is not working:
Because I am designing a control that has many Template objects (eg: RssChannelTitle,
RssChannelDescription, RssItemTitle, RssItemDescription, footer, etc.....)
I don't want them as primary (root level) Properties of the control, but as sub-properties within an
ExpandableObject, and so that the output xml/html would look more like:

<CC:MYCONTROL>
<TEMPLATES>
<HEADER>
My Custom Header Goes here!!!
</HEADER>
<FOOTER>
My custom footer goes here...
</FOOTER>
</TEAMPLATES>
</CC:MYCONTROL>


In other words (codewise):
System.ComponentModel.DefaultValue(null), /*Q: The right DefaultValue to give when not a null? */
System.ComponentModel.NotifyParentProperty(true),
System.Web.UI.PersistenceMode(System.Web.UI.PersistenceModeInnerProperty),
//System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
public Control_Templates Templates{get {return _Templates;}}
private _Templates = new Control_Templates();
where:

[TypeConverter(typeof(ExpandableObjectConverter))]
Control_Templates {
[
System.ComponentModel.DefaultValue(null),
System.ComponentModel.NotifyParentProperty(true),
System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty),
System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public ITemplate Header {get {return _Header;}set {_Header = value;}}
private ITemplate _Header = null;

[[[[[[[[[[[[[[[[...many more templates defs go here...]]]]]]]]]]]]]]]]

[
System.ComponentModel.DefaultValue(null),
System.ComponentModel.NotifyParentProperty(true),
System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty),
System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public ITemplate Footer {get {return _Footer;}set {_Footer = value;}}
private ITemplate _Footer = null;
}



The final result of this is that the XML generated looks like:

<CC:MYCONTROL>
<TEMPLATES>
<HEADER>
</HEADER>
<FOOTER>
</FOOTER>
</TEAMPLATES>
</CC:MYCONTROL>

even though all the ITemplates are NULL, rather than

<CC:MYCONTROL>
<TEMPLATES>
</TEAMPLATES>
</CC:MYCONTROL>

Which is causing rendering issues because during my Render() I wrote:

if (this.Templates.Header==null){this.Templates.Header = new Template_Header();}
if (this.Templates.Header!=null){
TemplateContainer_Header oHeader = new TemplateContainer_Header(this, this.oChannel);
this.Controls.Add(oHeader);
this.Templates.Header.InstantiateIn(oHeader);
}

But that doesn't work.
Now, because it has serialized the
property to being <HEADER></HEADER> it says that it has a header -- just that it's blank -- and
therefore never instantiates the builtin/default one.


The check in Render() would now have to be something like
if (this.Templates.Header.Controls.Count == 0) {...}
or something like that, which is absurd coding.


It would be best to figure out how to ensure that it doesn't write blank ITemplates as properties in
the xml....just like it can do
when the template is a base level property.

In other words, HELP!


(And Thank you!)




PS:
Tried so far -- modifying the attributes on one of the sub-properties :


[
System.ComponentModel.DefaultValue(null),
System.ComponentModel.NotifyParentProperty(true),
System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateContainer_Item))
]
public ITemplate Footer {get {return _Footer;}set {_Footer = value;}}
private ITemplate _Footer = null;

but to no avail:

I have tried removing the DesignerSerializationVisibility.Content:
I have tried removing the NotifiyParentProperty -- didn't work.
I have tried setting NotifyParentProperty to false... didn't work.
I have tried taking off the DefaultProperty(null)...didn't help either.

Out of ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top