views:

45

answers:

2

I want to pass complex information into a control. The equivalent of an entire XML document. What would be the best way to accomplish something like this:

<MyPrefix:MyControl runat="server">
  <Actions>
    <Action Name="Value" SomeParam="SomeValue" AnotherParam="AnotherValue"/>
    <Action Name="Value"/>
  </Action>
</MyPrefix:MyControl>

Could I just have an "Actions" property as a string, then wrap its contents in a root tag and parse it as XML?

Any guidance on the best practice here?

A: 

You can add a property like XmlFileName and read the associated file when you want.

Mehdi Golchin
No, because then I have manage a separate file for each instance of this control that have different params.
Deane
+1  A: 

Aha, got it

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace Unknown
{

    public class TestBuilder : ControlBuilder
    {

        public override bool AllowWhitespaceLiterals()
        {
            return false;
        }

        public override bool HtmlDecodeLiterals()
        {
            return true;
        }

    }

    [ToolboxData("<{0}:Test runat=\"server\" />"), DefaultProperty("Actions"), ParseChildren(true, "Actions"), ControlBuilder(typeof(TestBuilder))]
    public class Test : WebControl
    {

        [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty), Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public string Actions { get; set; }

        protected override void OnLoad(System.EventArgs e)
        {
            XDocument doc = XDocument.Parse("<Actions>" + this.Actions + "</Actions>");

            base.OnLoad(e);
        }

    }

}
Mehdi Golchin
Wow, nicely done. I actually ended up using child controls which each map to a class, this is really, really slick. Excellent work.
Deane