开发者

How can I add properties/attributes to an HTML markup container tag in ASP.Net

I've seen all of the usual pages with information about how to create a sub-tag that a开发者_运维百科llows 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.


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> 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜