locked
ITemplate parsing order issue RRS feed

  • Question

  • User925558938 posted

    Hi there,

    I've created a custom server control with a few ITemplate inner properties.  I'd like to allow for mixed use of these template tags like this:

    <x:CustomTag>
         <Tem1>First</Tem1>
         <Tem2>Second</Tem2>
         <Tem1>Third</Tem1>
         <Tem2>Fourth</Tem2>
    </x:CustomTag>

    Unfortunately, the ITemplate property setters are called in a different order than the template tags in the markup. The parser seems to see a Tem1 tag first, so it runs a setter for all the Tem1 tags, then for all the Tem2 tags. The result is: First, Third, Second, Fourth.

    Is there a way to retain the order of template tags when their setters are called, or alternately a way within the setter to find the location in the aspx of the tag that initiated the setter?  Or am I missing something?

    Thanks very much.

    Friday, August 12, 2011 6:49 PM

All replies

  • User3866881 posted

    Is there a way to retain the order of template tags when their setters are called, or alternately a way within the setter to find the location in the aspx of the tag that initiated the setter?  Or am I missing something?

    Hello:)

    Not very clear about what you say……

    Would you mind showing us your screenshot about the actual result and plz tell us what you are expecting....

    Thx again

    Saturday, August 13, 2011 11:48 PM
  • User925558938 posted

    Hi there,

    Here's an example:

    index.aspx:

    <%@ Register TagPrefix="tl" Namespace="CustomTagTest1" Assembly="CustomTagTest1" %>
    <tl:CustomTag runat="server">
        <Template1>First</Template1>
        <Template2>Second</Template2>
        <Template1>Third</Template1>
        <Template2>Fourth</Template2>
    </tl:CustomTag>

    CustomTag.cs:

    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace CustomTagTest1
    {
        [ParseChildren(true), PersistChildren(false)]
        public class CustomTag : WebControl
        {
            private List<ITemplate> list = new List<ITemplate>();
    
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [TemplateContainer(typeof(TemplateInstance))]
            public ITemplate Template1 { set { list.Add(value); } }
    
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [TemplateContainer(typeof(TemplateInstance))]
            public ITemplate Template2 { set { list.Add(value); } }
    
            protected override void CreateChildControls()
            {
                foreach (ITemplate t in list)
                {
                    TemplateInstance i = new TemplateInstance();
                    t.InstantiateIn(i);
                    Controls.Add(i);
                }
            }
        }
    
        public class TemplateInstance : WebControl, INamingContainer
        {
            protected override void Render(HtmlTextWriter writer) { RenderContents(writer); }
        }

    Expected Result:

    FirstSecondThirdFourth

    Actual Result:

    FirstThirdSecondFourth

    I would expect the parser to run the ITemplate setters in the order they exist in the markup, but it doesn't.  I can provide a more real-world example if this doesn't help clarify things.

    Thanks again!

    Monday, August 15, 2011 5:04 PM
  • User-16411453 posted

    There not custom tags

    It's a control collection.

    You have to build a control collection to properly access the internal tags. It takes about 3 pages of code to construct.

    Google control collection for more information. I would list examples, but it would be like 10 feet long in space.

    Wednesday, August 17, 2011 12:16 AM