Multiple Child Attributes
-
I found an article on MSDN that demonstrates how to parse child xml elements as a controls attributes and properties, like so: Here's the url for that: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconparsechildrenattributesample.asp My code is nearly identical to this, except I would like to be able to do multiple child attributes, like so: Is there any way to do this, or , perhaps someone might have a link that might explain how to do so? Thanks, --Keith
-
I found an article on MSDN that demonstrates how to parse child xml elements as a controls attributes and properties, like so: Here's the url for that: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconparsechildrenattributesample.asp My code is nearly identical to this, except I would like to be able to do multiple child attributes, like so: Is there any way to do this, or , perhaps someone might have a link that might explain how to do so? Thanks, --Keith
Hi there, + Yes, you can. As you already know that you can use the
ParseChildrenAttribute
to specify a default property of a custom control, and the page parser will interpret nested elements within the control's tag to create the value for the property. In the example provided in the MSDN document, they use a collection of homogeneous objects (Employee
), however, you can declare a collection of unhomogeneous objects like you are expecting now. What you need to do is to create a new class for the second type itemItemType2
, then you can declare it within the control's tag. + There's one more thing that as you want to define a collection of unhomogeneous objects, so you may want to create an abtract base class for the item of the control, and each item should inherit from the base class. You also want to create your own custom collection class instead of using the ArrayList. The sample code looks something like this:[ToolboxItem(false)]
public abstract class ControlItem
{
...
}public class ItemType1 : ControlItem
{
private string m_valuetype1;public string ValueType1 { get { return m\_valuetype1; } set { m\_valuetype1 = value; } }
}
public class ItemType2 : ControlItem
{
private string m_valuetype2;public string ValueType2 { get { return m\_valuetype2; } set { m\_valuetype2 = value; } }
}
public class ControlItemCollection : CollectionBase
{
...
}[ToolboxData("<{0}:SimpleControl runat=server></{0}:SimpleControl>")]
[ParseChildren(true, "Items")]
public class SimpleControl : System.Web.UI.WebControls.WebControl
{
private ControlItemCollection m_items;public SimpleControl() { m\_items = new ControlItemCollection(); } public ControlItemCollection Items { get { return m\_items; } } ...
}
You can declare the control in the web page at design time:
<cc1:SimpleControl id="SimpleControl1" runat="server">
<cc1:ItemType1 ValueType1="value1"></cc1:ItemType1>
<cc1:ItemType2 ValueType2="value2"></cc1:ItemType2>
</cc1:SimpleControl> -
Hi there, + Yes, you can. As you already know that you can use the
ParseChildrenAttribute
to specify a default property of a custom control, and the page parser will interpret nested elements within the control's tag to create the value for the property. In the example provided in the MSDN document, they use a collection of homogeneous objects (Employee
), however, you can declare a collection of unhomogeneous objects like you are expecting now. What you need to do is to create a new class for the second type itemItemType2
, then you can declare it within the control's tag. + There's one more thing that as you want to define a collection of unhomogeneous objects, so you may want to create an abtract base class for the item of the control, and each item should inherit from the base class. You also want to create your own custom collection class instead of using the ArrayList. The sample code looks something like this:[ToolboxItem(false)]
public abstract class ControlItem
{
...
}public class ItemType1 : ControlItem
{
private string m_valuetype1;public string ValueType1 { get { return m\_valuetype1; } set { m\_valuetype1 = value; } }
}
public class ItemType2 : ControlItem
{
private string m_valuetype2;public string ValueType2 { get { return m\_valuetype2; } set { m\_valuetype2 = value; } }
}
public class ControlItemCollection : CollectionBase
{
...
}[ToolboxData("<{0}:SimpleControl runat=server></{0}:SimpleControl>")]
[ParseChildren(true, "Items")]
public class SimpleControl : System.Web.UI.WebControls.WebControl
{
private ControlItemCollection m_items;public SimpleControl() { m\_items = new ControlItemCollection(); } public ControlItemCollection Items { get { return m\_items; } } ...
}
You can declare the control in the web page at design time:
<cc1:SimpleControl id="SimpleControl1" runat="server">
<cc1:ItemType1 ValueType1="value1"></cc1:ItemType1>
<cc1:ItemType2 ValueType2="value2"></cc1:ItemType2>
</cc1:SimpleControl>Wow. I couldn't have asked for a more detailed answer. Thanks so much! --Keith