FIX: You receive a "Multiple controls with the same ID" error message when you dynamically add cached user controls to Webforms

Symptoms

When you dynamically add cached user controls to an Webform, you receive an error message that is similar to the following when you browse to the Web page:
Multiple controls with the same ID '_909b6e15b33' were found. FindControl requires that controls have unique IDs.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



Exception Details: System.Web.HttpException: Multiple controls with the same ID '_909b6e15b33' were found. FindControl requires that controls have unique IDs.
Note The word "web" is capitalized incorrectly in this error message.

Note For more information about output caching and user controls, see the "References" section of this article.

Cause

This problem occurs because a unique name is passed to the LoadCachedControl function when dynamic controls are created from the cache. This unique name is intended to be the unique ID that is given to this instance of the dynamically loaded user control. However, a generated ID is used and the generated ID is not unique across the dynamically loaded user controls. This behavior causes the problem that is mentioned in the "Symptoms" section of this article.

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

837000 The PartialCachingControl.CachedControl property always returns a null reference

Resolution

ImportantThis hotfix contains two types of packages: a Microsoft Windows Installer package, and an Optional Component Manager package (OCM). If you use Windows Server 2003, you must specify that you require the Windows Server 2003 (OCM) update, because Windows Server 2003 File Protection monitors the .NET Framework 1.1 files. If you use an operating system that is different from Windows Server 2003, you must continue to use the Windows Installer package.

Software update information

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Microsoft .NET Framework 1.1 Service Pack 1 that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Restart requirement

You may have to restart your computer if any services or applications are running that use files that are listed in the "File information" section of this article.

File information

The English version of this software update has the file attributes (or later) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.

Date Time Version Size File name
--------------------------------------------------------------
04-Feb-2004 04:14 1.1.4322.996 258,048 Aspnet_isapi.dll
04-Feb-2004 04:14 1.1.4322.996 20,480 Aspnet_regiis.exe
04-Feb-2004 04:14 1.1.4322.996 32,768 Aspnet_state.exe
04-Feb-2004 04:14 1.1.4322.996 32,768 Aspnet_wp.exe
16-May-2003 00:49 33,522 Installpersistsqlstate.sql
16-May-2003 00:49 34,150 Installsqlstate.sql
04-Feb-2004 04:05 1.1.4322.996 94,208 Perfcounter.dll
04-Feb-2004 04:10 8,571 Smartnav.js
05-Feb-2004 05:11 1.1.4322.996 1,220,608 System.dll
05-Feb-2004 05:11 1.1.4322.996 241,664 System.messaging.dll
05-Feb-2004 05:11 1.1.4322.996 323,584 System.runtime.remoting.dll
05-Feb-2004 05:11 1.1.4322.996 131,072 System.runtime.serialization.formatters.soap.dll
05-Feb-2004 05:11 1.1.4322.996 1,257,472 System.web.dll
05-Feb-2004 05:11 1.1.4322.996 819,200 System.web.mobile.dll
05-Feb-2004 05:11 1.1.4322.996 569,344 System.web.services.dll
05-Feb-2004 05:11 1.1.4322.996 1,339,392 System.xml.dll
04-Feb-2004 04:10 14,482 Webuivalidation.js

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section of this article.

More Information

Steps to reproduce the problem

  1. Create the Web folder, for example, create the \inetpub\wwwrooot\PageCaching folder.
  2. Use the following code to create a user control that is named Test1.ascx, and then save it to the Web folder that was created in step 1:
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    <%@ Control Language="c#" "%>
    <asp:Label id="lblTest1" runat="server">Test 1 Control</asp:Label>&nbsp;
    <asp:Label id="lblDynamic" runat="server">Dynamic</asp:Label>
  3. Use the following code to create a user control that is named Test2.ascx, and then save it to the Web folder that was created in step 1:
    <%@ OutputCache Duration="60" VaryByParam="none" %>
    <%@ Control Language="c#" "%>
    <asp:Label id="lblTest1" runat="server">Test 2 Control</asp:Label>&nbsp;
    <asp:Label id="lblDynamic" runat="server">Dynamic</asp:Label>
  4. Use the following code to create a Web form that is named PageCaching.aspx, and then save it to the Web folder that was created in step 1:
    <script runat=server>
    void Page_Init(Object sender, EventArgs ev)
    {
    Control Pane = Page.FindControl("uc1");
    Control Item = (Control) LoadCachedControl("test1.ascx", "Dynamic1a");
    Pane.Controls.Add(Item);

    Pane = Page.FindControl("uc2");
    Item = (Control) LoadCachedControl("test2.ascx", "Dynamic2a");
    Pane.Controls.Add(Item);

    Pane = Page.FindControl("uc3");
    }

    public PartialCachingControl LoadCachedControl(string r_ControlName, string r_ControlID)
    {
    PartialCachingControl Control = (PartialCachingControl) Page.LoadControl(r_ControlName);
    Control.ID = r_ControlID;
    return Control;
    }
    </script>


    <%@ Page language="c#" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table>
    <tr>
    <td id="uc1" runat="server"></td>
    </tr>
    <tr>
    <td id="uc2" runat="server"></td>
    </tr>
    <tr>
    <td id="uc3" runat="server"></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
  5. Browse to http://localhost/PageCaching/PageCaching.aspx. You receive the error message that is described in the "Symptoms" section of this article.
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates

Using the PartialCachingControl class

The PartialCachingControl class has a CacheControl property. The CacheControl property can be used to determine if a user control has already been cached. The CacheControl property returns the user control only when the output of the user control output is not cached. If the user control output exists in the output cache, the CacheControl property returns a null reference because the control is never instantiated (in Microsoft Visual Basic, the CacheControl property returns Nothing). However, the CacheControl property always returns FALSE if the PartialCachingControl class is not in the control tree.

References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

306459 ASP.NET server controls overview

308378 How to perform fragment caching in ASP.NET by using Visual C# .NET

308645 How to perform fragment caching in ASP.NET by using Visual Basic .NET

Properties

Article ID: 834608 - Last Review: Feb 15, 2017 - Revision: 3

Feedback