tags:

views:

348

answers:

3

I would like to make my web control more readable in design mode, basically I want the tag declaration to look like:

<cc1:Ctrl ID="Value1" runat="server">        
     <Values>string value 1</Value>
     <Values>string value 2</Value>
</cc1:Ctrl>

Lets say I have a private variable in the code behind:

List<string> values = new List<string>();

So how can I make my user control fill out the private variable with the values that are declared in the markup?

A: 

I see two options, but both depend on your web control implementing some sort of collection for your values. The first option is to just use the control's collection instead of your private variable. The other option is to copy the control's collection to your private variable at run-time (maybe in the Page_Load event handler, for example).

Say you have web control that implements a collection of items, like a listbox. The tag looks like this in the source view:

    <asp:ListBox ID="ListBox1" runat="server">
        <asp:ListItem>String 1</asp:ListItem>
        <asp:ListItem>String 2</asp:ListItem>
        <asp:ListItem>String 3</asp:ListItem>
    </asp:ListBox><br />

Then you might use code like this to load your private variable:

        List<String> values = new List<String>();

        foreach (ListItem item in ListBox1.Items)
        {
            values.Add(item.Value.ToString());
        }

If you do this in Page_Load you'll probably want to only execute on the initial load (i.e. not on postbacks). On the other hand, depending on how you use it, you could just use the ListBox1.Items collection instead of declaring and initializing the values variable.

I can think of no way to do this declaratively (since your list won't be instantiated until run-time anyway).

Matt
A: 

Sorry I should have been more explicit. Basically I like the functionality that the ITemplate provides (http://msdn.microsoft.com/en-us/library/aa719834.aspx)

But in this case you need to know at runtime how many templates can be instansitated, i.e.

void Page_Init() {
if (messageTemplate != null) {
    for (int i=0; i<5; i++) {
        MessageContainer container = new MessageContainer(i);
        messageTemplate.InstantiateIn(container);
        msgholder.Controls.Add(container);
    }
}

}

In the given example the markup looks like:

<acme:test runat=server>
   <MessageTemplate>
    Hello #<%# Container.Index %>.<br>
   </MessageTemplate>
</acme:test>

Which is nice and clean, it does not have any tag prefixes etc. I really want the nice clean tags.

I'm probably being silly in wanting the markup to be clean, I'm just wondering if there is something simple that I'm missing.

Daniel Pollard
Ha! I should have known this question wasn't as easy as it seemed. ;)
Matt
+2  A: 

I think what you are searching for is the attribute:

[PersistenceMode(PersistenceMode.InnerProperty)]

Persistence Mode

Remember that you have to register your namespace and prefix with:

<%@ Register Namespace="MyNamespace" TagPrefix="Pref" %>
volothamp