6

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.

Kashif
  • 14,071
  • 18
  • 66
  • 98

4 Answers4

9

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!

Thys
  • 149
  • 1
  • 5
8

I used this code to fix my problem, notice the ScriptMode is set to "Release"

<AjaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"  ScriptMode="Release">
</AjaxControlToolkit:ToolkitScriptManager>

I see a similar answer from this link: http://www.advancesharp.com/questions/17658/sys-invalidoperationexception-two-components-with-the-same-id-xxx-can-t-be-added-to-the-application

Tuyen Nguyen
  • 4,389
  • 7
  • 51
  • 77
3

Remove BehaviorID property from extender

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
2

Similar issue here. The solution for me was to change the Script Manager from a shortcut close tag to the full close tag , after adding the ScriptMode="Release" attribute:

Change: <asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server" />

to: <asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server></asp:ScriptManager>

David H
  • 21
  • 2