Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Multiple Child Attributes

Multiple Child Attributes

Scheduled Pinned Locked Moved ASP.NET
htmlcomxmltutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SilentSeraph
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • S SilentSeraph

      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

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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 item ItemType2, 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>

      S 1 Reply Last reply
      0
      • M minhpc_bk

        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 item ItemType2, 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>

        S Offline
        S Offline
        SilentSeraph
        wrote on last edited by
        #3

        Wow. I couldn't have asked for a more detailed answer. Thanks so much! --Keith

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups