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

<pre><code>&lt;cc1:Ctrl ID="Value1" runat="server"&gt;        
     &lt;Values&gt;string value 1&lt;/Value&gt;
     &lt;Values&gt;string value 2&lt;/Value&gt;
&lt;/cc1:Ctrl&gt;
</code></pre>

<p>Lets say I have a private variable in the code behind:</p>

<pre><code>List&lt;string&gt; values = new List&lt;string&gt;();
</code></pre>

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

<hr>

<p>Sorry I should have been more explicit. Basically I like the functionality that the ITemplate provides (<a href="http://msdn.microsoft.com/en-us/library/aa719834.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa719834.aspx</a>)</p>

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

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

<p>}</p>

<p>In the given example the markup looks like:</p>

<pre><code>&lt;acme:test runat=server&gt;
   &lt;MessageTemplate&gt;
    Hello #&lt;%# Container.Index %&gt;.&lt;br&gt;
   &lt;/MessageTemplate&gt;
&lt;/acme:test&gt;
</code></pre>

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

<p>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.</p>

[{'answer_id': 61925, 'author': 'Matt', 'author_id': 4154, 'author_profile': 'https://Stackoverflow.com/users/4154', 'pm_score': 0, 'selected': False, 'text': '<p>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).</p>\n\n<p>Say you have web control that implements a collection of items, like a listbox.  The tag looks like this in the source view:</p>\n\n<pre><code>    &lt;asp:ListBox ID="ListBox1" runat="server"&gt;\n        &lt;asp:ListItem&gt;String 1&lt;/asp:ListItem&gt;\n        &lt;asp:ListItem&gt;String 2&lt;/asp:ListItem&gt;\n        &lt;asp:ListItem&gt;String 3&lt;/asp:ListItem&gt;\n    &lt;/asp:ListBox&gt;&lt;br /&gt;\n</code></pre>\n\n<p>Then you might use code like this to load your private variable:</p>\n\n<pre><code>        List&lt;String&gt; values = new List&lt;String&gt;();\n\n        foreach (ListItem item in ListBox1.Items)\n        {\n            values.Add(item.Value.ToString());\n        }\n</code></pre>\n\n<p>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.</p>\n\n<p>I can think of no way to do this declaratively (since your list won\'t be instantiated until run-time anyway).</p>\n'}, {'answer_id': 62589, 'author': 'Luca Molteni', 'author_id': 4206, 'author_profile': 'https://Stackoverflow.com/users/4206', 'pm_score': 3, 'selected': True, 'text': '<p>I think what you are searching for is the attribute:</p>\n\n<pre><code>[PersistenceMode(PersistenceMode.InnerProperty)]\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.persistencemode.aspx" rel="nofollow noreferrer">Persistence Mode</a></p>\n\n<p>Remember that you have to register your namespace and prefix with:</p>\n\n<pre><code>&lt;%@ Register Namespace="MyNamespace" TagPrefix="Pref" %&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/61861', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2758/']