[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

AddParsedSubObject and Multiple Identical Property Types

Mark Olbert

2/24/2007 5:44:00 PM

I have a custom composite control decorated with ParseChildren(false) and PersistChildren(true) because I want to parse the inner
markup for custom controls. That part works fine.

But the "top level" control also contains a series of style properties that I want to persist using inner markup, too.

I can handle the parsing by building a custom builder and overriding GetChildControlType. But what gets passed to AddParsedSubObject
is just an instance of the Type returned by GetChildControlType. Since there are several style properties -- all of the same Type --
how do I distinguish between them in AddParsedSubObject? For example:

public TableStyle UniqueStyle
{
}

public TableItemStyle FirstStyle
{
}

public TableItemStyle SecondStyle
{
}

protected override void AddParsedSubObject( object obj )
{
TableStyle tableStyle = obj as TableStyle;
if( tableStyle != null )
{
UniqueStyle = tableStyle;
return;
}

TableItemStyle itemStyle = obj as TableItemStyle;
if( itemStyle != null )
{
// oops, there are two properties this could be!
}
}

Since no other identifying information on the object gets passed to AddParsedSubObject, how do I distinguish between properties of
the same Type?

My current workaround is define a bunch of derived Types so as to make each style property unique...but that seems ugly. And I think
it's causing me a problem in the design-time environment with the display not being updated when a style element changes, so I'd
like to use a different approach.

- Mark
4 Answers

wawang

2/26/2007 8:49:00 AM

0

Hi Mark,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

wawang

2/27/2007 9:01:00 AM

0

Hi Mark,

I've consulted your question with product team. It seems creating a custom
control builder here is not the best way to create custom persistent markup
for the properties.

It seems my suggestion to create a such custom control builder is not very
appropriate here, sorry for the inconvenience caused.

The correct and recommended way is to use a TypeConverter to create the
markup you wanted. Using the Color for example, here's how we do it in
ASP.NET's WebControl class:

[TypeConverter(typeof(WebColorConverter)),
DefaultValue(typeof(Color), ""),
WebCategory("Appearance"),
WebSysDescription("WebControl_BackColor")]
public virtual Color BackColor
{
get
{
if (!this.ControlStyleCreated)
{
return Color.Empty;
}
return this.ControlStyle.BackColor;
}
set
{
this.ControlStyle.BackColor = value;
}
}

To persist a collection of them, I recommend following the example on this
site: http://www.leftslipper.com/ShowFaq.asp...

Parsing and persisting controls is almost always a job best left to ASP.NET.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Mark Olbert

2/27/2007 3:04:00 PM

0

Walter,

Thanks for the response, but I don't see how that would be practical when I'm persisting 12 styles, each of which may have any
number of a number of subproperties set. Is there a TypeConverter for Style objects in the Framework that I've missed? Even if there
is, I'm not sure I'd want to use it -- being a TypeConverter, all of the markup would get put into the tag attributes, which would
get >>very<< long :).

There must be a different answer available. There are a number of MS-provided controls that do exactly what I want to do. For
example, the Wizard control persists its style properties as inner properties of the Wizard tag. How is the parsing and persisting
of those properties handled?

- Mark

wawang

3/1/2007 10:19:00 AM

0

Hi Mark,

The TableItemStyle inherits from Style which associated a TypeConverter.

Here's a simple example on how to create a customized TypeConverter for a
custom property class:

public class MyTypeConverter : ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType != typeof(string))
{
throw base.GetConvertToException(value, destinationType);
}
return string.Empty;
}
}

[TypeConverter(typeof(MyTypeConverter))]
public class MyProp2 : IStateManager
{
private string _name;

[NotifyParentProperty(true)]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _value;

[NotifyParentProperty(true)]
public string Value
{
get { return _value; }
set { _value = value; }
}

#region IStateManager Members

private bool _marked;

bool IStateManager.IsTrackingViewState
{
get { return _marked; }
}

void IStateManager.LoadViewState(object state)
{
if (state != null)
{
Pair p = (Pair)state;
_name = (string)p.First;
_value = (string)p.Second;
}
}

object IStateManager.SaveViewState()
{
return new Pair(_name, _value);
}

void IStateManager.TrackViewState()
{
_marked = true;
}

#endregion
}

public class MyControl2 : CompositeControl
{
private TableItemStyle _firstStyle;

[

DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public TableItemStyle FirstStyle
{
get
{
if (_firstStyle == null)
{
_firstStyle = new TableItemStyle();
if (base.IsTrackingViewState)
{
((IStateManager)_firstStyle).TrackViewState();
}
}
return _firstStyle;
}
}

private MyProp2 _myprop2;

[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public MyProp2 MyProp2
{
get
{
if (_myprop2 == null)
{
_myprop2 = new MyProp2();
if (base.IsTrackingViewState)
{
((IStateManager)_myprop2).TrackViewState();
}
}
return _myprop2;
}
}

}



#A Crash Course on ASP.NET Control Development: Building New Controls from
the Ground Up
http://msdn2.microsoft.com/en-us/library/aa4...

#A Crash Course on ASP.NET Control Development: Building Data-Bound Controls
http://msdn2.microsoft.com/en-us/library/aa4...

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.