(ModalPopupExtender) two components with the same id can't be added to the application

Kashif picture Kashif · Nov 1, 2012 · Viewed 9k times · Source

I have a user control which I add on a page whenever user click on button. Following is the code to add control.

protected void Page_Init(object sender, EventArgs e)
{
    if (Session["ControlCount"] != null)
    {
        for (int i = 1; i <= (int)Session["ControlCount"]; i++)
        {
            Control myUserControl = LoadControl("~/Controls/MessageControl.ascx");
            divMessageControl.Controls.Add(myUserControl);
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnExpand_Click(object sender, EventArgs e)
{
    int count = 0;
    if (Session["ControlCount"] != null)
    {
        count = Convert.ToInt32(Session["ControlCount"]);
    }

    Control myUserControl = (Control)Page.LoadControl("~/Controls/MessageControl.ascx");
    divMessageControl.Controls.Add(myUserControl);
    Session["ControlCount"] = count + 1;
}

This control has ModalPopupExtender popup. When I add 2nd control on page it throws an error internally which i can see in firebug. How to make this popup id unique?

<asp:ModalPopupExtender ID="mpeReply" BehaviorID="mpeReply" runat="server" TargetControlID="btnReply"
    PopupControlID="pnlReply" BackgroundCssClass="ModalPopupBG1">
</asp:ModalPopupExtender>

Sys.InvalidOperationException: Sys.InvalidOperationException: Two components with the same id 'mpeReply' can't be added to the application.

Answer

Thys picture Thys · May 22, 2013

I have found the solution to this problem, as much as a lot of people have said, the simple solution is that your HTML is not properly formed - there is either an extra or missing closing tag to an element. Make sure that all your tags are properly closed and the problem should go away - struggled all day with this one!