<p>We have a couple of ASP.Net dataview column templates that are dynamically added to the dataview depending on columns selected by users.</p>

<p>These templated cells need to handle custom databindings:</p>

<pre><code>public class CustomColumnTemplate: 
    ITemplate
{
    public void InstantiateIn( Control container )
    {
        //create a new label
        Label contentLabel = new Label();

        //add a custom data binding
        contentLabel.DataBinding +=
            ( sender, e ) =&gt;
            {
                //do custom stuff at databind time
                contentLabel.Text = //bound content
            };

        //add the label to the cell
        container.Controls.Add( contentLabel );
    }
}

...

myGridView.Columns.Add( new TemplateField
    {
       ItemTemplate = new CustomColumnTemplate(),
       HeaderText = "Custom column"
    } );
</code></pre>

<p>Firstly this seems rather messy, but there is also a resource issue. The <code>Label</code> is generated, and can't be disposed in the <code>InstantiateIn</code> because then it wouldn't be there to databind.</p>

<p>Is there a better pattern for these controls? </p>

<p>Is there a way to make sure that the label is disposed after the databind and render?</p>

[{'answer_id': 30536, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 3, 'selected': True, 'text': '<p>I have worked extensively with templated control and I have not found a better solution.</p>\n\n<p>Why are you referencing the contentLable in the event handler?</p>\n\n<p>The sender is the label you can cast it to the label and have the reference to the label.  Like below.</p>\n\n<pre><code>        //add a custom data binding\n        contentLabel.DataBinding +=\n            (object sender, EventArgs e ) =&gt;\n            {\n                //do custom stuff at databind time\n                ((Label)sender).Text = //bound content\n            };\n</code></pre>\n\n<p>Then you should be able to dispose of the label reference in InstantiateIn.</p>\n\n<p>Please note I have not tested this.</p>\n'}, {'answer_id': 14066402, 'author': 'jackvsworld', 'author_id': 1097054, 'author_profile': 'https://Stackoverflow.com/users/1097054', 'pm_score': 1, 'selected': False, 'text': '<p>One solution is to make your template <em>itself</em> implement <code>IDisposable</code>, and then dispose the controls in your template\'s <code>Dispose</code> method. Of course this means you need some sort of collection to keep track of the controls you\'ve created. Here is one way to go about it:</p>\n\n<pre><code>public class CustomColumnTemplate :\n    ITemplate, IDisposable\n{\n    private readonly ICollection&lt;Control&gt; labels = new List&lt;Control&gt;();\n\n    public void Dispose()\n    {\n        foreach (Control label in this.labels)\n            label.Dispose();\n    }\n\n    public void InstantiateIn(Control container)\n    {\n        //create a new label\n        Label contentLabel = new Label();\n\n        this.labels.Add(contentLabel);\n</code></pre>\n\n<p>...</p>\n\n<pre><code>        //add the label to the cell\n        container.Controls.Add( contentLabel );\n    }\n}\n</code></pre>\n\n<p>Now you are still faced with the problem of disposing the template. But at least your template will be a responsible memory consumer because when you call <code>Dispose</code> on the template all of its labels will be disposed with it.</p>\n\n<p><strong>UPDATE</strong></p>\n\n<p><a href="http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/c02bde1c-6fbd-4d25-8727-b52c2e6ceb88" rel="nofollow">This link on MSDN</a> suggests that perhaps it is not necessary for your template to implement <code>IDisposable</code> because the controls will be rooted in the page\'s control tree and automatically disposed by the framework!</p>\n'}]
['https://Stackoverflow.com/questions/29976', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/905/']