Matching IDs in Update Panel within a Repeater - “

2019-07-22 08:18发布

问题:

I have question about why 2 controls in separate repeaters cannot have the same ID if they are within an update panel, but they can share the same ID if they are not in an update panel. See this code...

    <asp:Repeater ID="rptFirstRepeater" runat="server">
        <ItemTemplate>
            <asp:Image runat="server" ID="imgThisWorks" />
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <asp:Image runat="server" ID="imgThisDoesntWork" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </ItemTemplate>
    </asp:Repeater>

    <asp:Repeater ID="rptSecondRepeater" runat="server">
        <ItemTemplate>
            <asp:Image runat="server" ID="imgThisWorks" />
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <asp:Image runat="server" ID="imgThisDoesntWork" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </ItemTemplate>
    </asp:Repeater>

Generates this error:

CS0102: The type 'ASP._8_admin_testemail_aspx' already contains a definition for 'imgThisDoesntWork'

But it works fine if you don't use the update panel, like so.

    <asp:Repeater ID="rptFirstRepeater" runat="server">
        <ItemTemplate>
            <asp:Image runat="server" ID="imgThisWorks" />
        </ItemTemplate>
    </asp:Repeater>

    <asp:Repeater ID="rptSecondRepeater" runat="server">
        <ItemTemplate>
            <asp:Image runat="server" ID="imgThisWorks" />
        </ItemTemplate>
    </asp:Repeater>

I understand that all controls within repeaters are given new ids something lke... ctl00_cttBody_ucTestControl_rptFirstRepeater_ctl00_imgThisWorks

Does this not apply to the update panel as well? Would I be able to make the code above work using the same IDs? - please ignore the fact that these 2 repeaters should really be one repeater! :)

Thanks, charles.

回答1:

It seems to be a known bug which microsoft has decided not to fix:

http://connect.microsoft.com/VisualStudio/feedback/details/417230/updatepanel-breaks-naming-containers-compile-time-bug

At this point in time, we've decided not to fix this specific issue. Fortunately, there's a very simple workaround - avoid using the same control ID inside and outside the UpdatePanel.