开发者

xml-like properties in a user control declaration

I've created a user control that takes content from an XML file and renders the content on the page. Fairly straight-forward stuff.

However it's come up that I may need to replace portions of the content based on id with other manual content.

My idea is to expose a repeatable property within the user control's declaration like this:

<my:XmlRenderSource ID="XmlRenderSource1" runat="server" XmlUrl="xml/sample.xml">
    <OverrideContent开发者_运维技巧 targetId='thingToReplaceId'><p>New Content</p></OverrideContent>
    <OverrideContent  targetId='thingToReplaceId2'><p>New Content</p></OverrideContent>
</my:XmlRenderSource>

So far I have the following in my userControl (i've trimmed out the useless stuff):

 public class OverrideContent
    {
        public string targetId { get; set; }
    }

    public class OverrideContentCollection : List<OverrideContent>
    {

    }


[
      ParseChildren(
  typeof(OverrideContent),
  DefaultProperty = "OverrideItems",
  ChildrenAsProperties = true
  )
  ]
public partial class XmlRenderSource : System.Web.UI.UserControl
    {
        private string xmlUrl = "";
        private string xmlUrlBase = "";

          public OverrideContentCollection OverrideItems
          {
          get;
          set;
          }
// Loads of other code that doesn't matter for this
}

Where on load or pre render I loop through the OverrideContent items and replace portions of the xml before render. I understand how I can do that, but I am having a distinct problem exposing the OverrideContent items as a repeatable property.

I know it can be done, but for the life of me I cannot get it going. If anyone could provide a crash-course on how to do this, I would be eternally grateful.


You can try with something like that:

[ParseChildren(typeof(OverrideContent), DefaultProperty = "OverrideItems", ChildrenAsProperties=true)]
public partial class XmlRenderSource : System.Web.UI.UserControl
{
    private OverrideContentCollection overrideItems = new OverrideContentCollection();

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public OverrideContentCollection OverrideItems
    {
        get { return overrideItems; }
    }
}

I used this page as a test:

<%@ Register Src="XmlRenderSource.ascx" TagName="XmlRenderSource" TagPrefix="uc1" %>
<%@ Register Namespace="WebApplication2" TagPrefix="uc1" Assembly="WebApplication2" %>
...

<uc1:XmlRenderSource ID="XmlRenderSource1" runat="server">
    <uc1:OverrideContent targetId="test">Content</uc1:OverrideContent>
    <uc1:OverrideContent targetId="test2" />
</uc1:XmlRenderSource>

EDIT: If you need to put some text into each OverrideElement, this is one way to do it (I also updated the test page above):

[ParseChildren(true, "text")]
public class OverrideContent
{
    public string targetId { get; set; }

    [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
    public string text { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜