Search code examples
c#asp.netweb-controls

How to remove controls with runat="server" specified in asp.net


I have a webpage with server accessible controls, see 'FileIconLink' below:

<body>
    <p class="FirstTitle style5">Downloads:</p>
    <div id="BreadcrumbDiv">
        <p style="padding-left:5px; ">Page Loading...</p>
    </div><!--/BreadcrumbDiv-->
    <div id="DirLinksDiv">
        <p><span class="SecondTitle">Files:</span></p>
            <a runat="server" href="#" id="FileIconLink">File</a>
            <% WriteFileLinks(); %>
        <p><span class="SecondTitle">Folders:</span></p>
            <a runat="server" href="#" id="FolderIconLink">Folder</a>
    </div><!--/DirLinksDiv-->
</body>
<%RemoveHTMLTemplates(); %>

Both 'FileIconLink' and 'FolderIconLink' are templates of web controls which are copied by my code - such as <% WriteFileLinks(); %> above. How could these templates be permanently removed from the web page at run-time on the server without causing the error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Thanks in advance!


Solution

  • Ultimately I realised my approach was wrong, as Cade Roux was alluding to, I needed to make up my mind where the templates were going to be used.

    My solution was as follows:

    • Make controls for containing the results of my (previously inline) code.
    • Use templates in Page_Load to fill the controls described above.
    • Delete templates in Page_Load.
    • Do nothing inline.