0

I've seen all of the usual pages with information about how to create a sub-tag that allows content within a user control (using ITemplate and INamingContainer) but I've yet to see anyone able to add properties that become attributes to said tags, for example:

<asp:MyControl runat="server" ID="myControlTest" SomeAttribute="SomeValue">
    <Content ContentAttribute="Something">
        <a href="SomePage.aspx" alt="Blah">Blah</a>
    </Content>
</asp:MyControl>

If you see the ContentAttribute on the Content tag, that is what I'd like to be able to achieve, but if I set it all up using ITemplate and INamingContainer etc, I can add a property that does in fact appear in Intellisense for that tag but when I run the code, it says Content does not have property/attribute named ContentAttribute (it also gives the same as a warning in VS IDE but still allows me to compile it).

I have tried everything to make this work and so far the only way seems to be if I make the Content property on MyControl a class that inherits from System.Web.UI.Control and implements ITemplate. That works but unfortunately I have to specify the runat attribute on the Content tag (because it sees it as a control rather than a sub-tag) and I'd rather not do that if possible.

Hope I have explained this well enough, if I haven't please let me know and I'll do my best to elaborate further.

Thanks in advance.

1 Answer 1

1

I think what you're proposing is something like a MIME email where there are a variable number of sections, each with an identifier for the client to choose the best version of the email it can handle. I assume you're wanting to select the appropriate template at runtime, based on that attribute.

The standard .NET controls don't implement that way, so far as I can tell. Think of the Repeater which has:

<asp:Repeater id="myRepeater" runat="server">
  <HeaderTemplate>...</HeaderTemplate>
  <ItemTemplate>...</ItemTemplate>
  <FooterTemplate>...</FooterTemplate>
</asp:Repeater>

Each of the subitems (templates) has a different name, not the same name with a separate attribute.

Is there any way for you to define, ahead of time, what all of the possible sections might be, the way the repeater does?

<asp:MyControl runat="server" ID="myCtlTest">                            
  <SomethingTemplate><a href="SomePage.aspx" alt="Blah">Blah</a></SomethingTemplate>
  <OtherTemplate><a href="OtherPage.aspx" alt="Blah">Blah</a></OtherTemplate>
</asp:MyControl> 

I'm guessing not but wanted to throw it out there in case.

Alternately, could the ContentAttribute move to MyControl? The SETter would then load/build the template for you depending on the value.

<asp:MyControl runat="server" ID="myCtlTest" ContentAttribute="Something">
  <Template></Template>                        
</asp:MyControl> 

...or it could be loaded with a method instead of using the property SETter.

If you will always need multiple templates, perhaps a combination of those two concepts would help.

<asp:MyControl runat="server" ID="myControlTest" 
               SomethingTemplate="Something"
               OtherTemplate="Other">
  <SomethingTemplate></SomethingTemplate>                        
  <OtherTemplate></OtherTemplate>                        
</asp:MyControl> 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Steve, thanks for the suggestions - definitely a lot I didn't consider there and they are very good suggestions. As to your question about whether or not I would know the sections beforehand, I would, but I would not know the content of those sections. I could put the attributes on the parent tag, but I lose the logic, since it would appear odd to have such attributes on the parent and also I want to make the content tag a generic class I can use in multiple user-controls and therefore I would have to add those attributes to each control that used this generic content tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.