-1

I have many UserControls and I wanna load them depended on query string in one aspx page.

I tired 2 following ways:

  1. Add a Placeholder in aspx page and in Page_Load/Page_Render event:

    UserControl uc = (UserControl)LoadControl("/PATH/ProductGroups.ascx");
    phMain.Controls.Add(uc);
    
  2. Add a MasterPage, then add an aspx and using of masterpage, then register the UserControl on the aspx

But in both, I got following error:

The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

BTW, I have used telerik TextEditor in the loaded UserControl.

How I can handle that?

I think the only way that I have, multiple aspx per UserContorl, without MasterPage, this is sucks, as you know ! because I have to fix HTML/CSS/JS in all pages, one by one and this is not pro !

NOTE: without using telerik TextEditor, everything working fine, but I need this one, also this wat (loading UserControl in PlaceHolder) is not really good way.

I'm looking for something like DNN.

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64

1 Answers1

1

I tried to replicate this error, but i couldn't. It is just working fine for me. Here is what I tried in VS2013.

1) Added MasterPage

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Test.Site1" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

2)Added WebUserControl

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="Test.WebUserControl1" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<telerik:RadEditor  runat="server" ID="RadEditor1" SkinID="DefaultSetOfTools" Height="675px">
</telerik:RadEditor>

3) Added WebForm

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Test.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:PlaceHolder runat="server" ID="phMain" />
</asp:Content>

4) Loaded the control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserControl uc = (UserControl)LoadControl("~/WebUserControl1.ascx");
            phMain.Controls.Add(uc);
        }
    }
}

Here is the resolution for the error "Control collection cannot be modified"

Community
  • 1
  • 1
Nirav Vyas
  • 131
  • 5
  • Could you please test your code, using placeholder (no master page) – Mehdi Dehghani Aug 13 '15 at 16:40
  • 1
    Yes this is also working fine for me. If you are not using master page then make sure that you add telerik:RadScriptManager on that page inside form tag. If you still get same error "The control collection cannot be modified", make sure the head tag is formatted as "" – Nirav Vyas Aug 13 '15 at 17:37
  • no, I replaced telerik texteditor with ckeditor, and used master-page, but thanks! – Mehdi Dehghani Aug 18 '15 at 08:05