Matching IDs in Update Panel within a Repeater - “already contains a definition for…”

二次信任 提交于 2019-12-11 00:21:30

问题


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.



来源:https://stackoverflow.com/questions/9330217/matching-ids-in-update-panel-within-a-repeater-already-contains-a-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!