This is a migrated thread and some comments may be shown as answers.

UserControl & Controls collection cannot be modified...

4 Answers 62 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jesús
Top achievements
Rank 1
Jesús asked on 24 Nov 2008, 03:08 PM
Hi.

I have a page with an Ajax Manager that manages a RadMenu, a RadToolbar, a panel, that contains an UserControl, and a RadGrid, and the content of the UserControl is RadGrid dependant.

The trouble I have is when the page is building to be displayed I have the error message

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)

What happens? In the UserControl I have some javascript code, that refers to concrete RadInput or RadDatePicker controls in this UserControl, and its dependant of some QueryString parameter (the aspx page is always the same). In some forums I've read I have to refer the controls with <%# %> instead of <%= %>, or I have to move the javascript code to head section in the main page, but I'm working with an UserControl (ascx file, with no head section).

A sample aspx code I have is:

<head id="header" runat="server"
    <title>FocusOnRisk - DetailForm &#946</title> 
    <link href="Styles/FocusOnRisk.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
    <form id="form1" runat="server"
        <div> 
            <script type="text/javascript"
            function OnResponseEndHandler(sender, arguments) 
            { 
                <%= toolBar.ClientID %>.attachEvent("OnClientClick","click_handler"); 
            } 
            </script> 
 
            <radA:RadAjaxManager ID="ajaxMngr" runat="server" ClientEvents-OnResponseEnd="OnResponseEndHandler"
                ... 
            </radA:RadAjaxManager> 
            <asp:Panel ID="menuPanel" runat="server"></asp:Panel> 
            <div style="text-align: right"
                <asp:Panel ID="toolBarPanel" runat="server"
                    <radTlb:RadToolbar ID="toolBar" runat="server" UseFadeEffect="True" Skin="mac" ButtonHeight="24px" 
                        ButtonWidth="24px" AutoPostBack="true" CatalogIconImageUrl="" SkinsPath="~/RadControls/Toolbar/Skins" 
                        ImagesDir="~/Imagenes/" OnOnClick="toolBar_OnClick" OnPreRender="toolBar_PreRender" 
                        ValidationGroup="GV1"
                    </radTlb:RadToolbar> 
                    <script type="text/javascript"
                <!-- 
                <%= toolBar.ClientID %>.attachEvent("OnClientClick","click_handler"); 
                function click_handler(sender, e) 
                { 
                    if (sender.CommandName=='DELETE') { 
                        return confirm('<%= MissatgeDinamic("Del1") %>'); 
                    } 
                } 
           --> 
                    </script> 
                </asp:Panel> 
            </div> 
            <asp:Panel ID="detailsPanel" runat="server"
            </asp:Panel> 
            <br /> 
            <radG:RadGrid ID="listGrid" runat="server" PageSize="6" Skin="Glassy" OnNeedDataSource="listGrid_NeedDataSource" 
                OnItemCreated="listGrid_ItemCreated" OnSelectedIndexChanged="listGrid_SelectedIndexChanged" 
                AllowPaging="True" AllowAutomaticDeletes="false" AllowAutomaticInserts="false" 
                AllowAutomaticUpdates="false"
                ... 
            </radG:RadGrid> 
        </div> 
    </form> 
</body> 

and the detailsPanel is where the UserControl will be loaded. A UserControl sample code is:

<script type="text/javascript"
function cambiarFecha() 
    var input1 = <%=fechaini.ClientID %>
    var input2 = <%=fechafin.ClientID %>
    var today = input1.GetDate();  
    var thisYear = today.getYear();  
    var thisMonth = today.getMonth();  
    var thisDay = today.getDate(); 
    var maxDate; 
    if (thisDay <= 15) { 
       maxDate = new Date(thisYear, thisMonth, 15); 
    } 
    else 
    { 
       maxDate = new Date(thisYear, thisMonth+1, 0); 
    } 
    input2.SetDate(maxDate); 
    input2.SetMaxDate(maxDate); 
    input2.SetMinDate(input1.GetDate()); 
</script> 
 
<table id="Table1"
    <tr> 
        <td><asp:Label ID="lbl_fechaini" runat="server" /></td
        <td><radCln:RadDatePicker ID="fechaini" runat="server" Width="150" DateInput-ReadOnly="true" 
                DateInput-OnClientDateChanged="cambiarFecha" /> 
        </td> 
    </tr> 
    <tr> 
        <td><asp:Label ID="lbl_fechafin" runat="server" /></td
        <td> 
            <radCln:RadDatePicker ID="fechafin" runat="server" Width="150" DateInput-ReadOnly="true" /> 
        </td> 
    </tr> 
</table> 
 

and in the behind code I search at my database for if some fields needs more controls, like required field validators. When at database I detect this I create the control I will add and finally I have a line like this:

foreach (Control aux in ctrlAux) 
  Controls.AddAt(ctrlAuxPos[idx], aux); 
  idx++; 

where is the exception I have is throwed.

4 Answers, 1 is accepted

Sort by
0
Paul
Telerik team
answered on 24 Nov 2008, 03:15 PM
Hello Jesus,

Try to wrap you JS functions inside RadScriptBlock.

<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
<script type="text/javascript">  
function ......  
</script> 
</telerik:RadScriptBlock> 


All the best,
Paul
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jesús
Top achievements
Rank 1
answered on 24 Nov 2008, 03:20 PM
We are using RadControls for ASP.NET Q2 2007, and here there isn't the RadScriptBlock.

We are finnishing some changes before upgrade to the current RadControls version.
0
Paul
Telerik team
answered on 05 Dec 2008, 09:11 AM
Hi Jesus,

I think it will be best if you can open a support ticket and send us a simple running project (incl. CSS, images, skins, DB backup if needed and so on) demonstrating the problem (and step-by-step instructions on doing so). In that way we can reproduce and pinpoint the problems you're facing on our side, understand the logic of your application and provide a solution.

Thanks beforehand for your patience and cooperation,
Paul
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Jesús
Top achievements
Rank 1
answered on 05 Dec 2008, 12:08 PM
Currently we are migrating to Q3 2008.
Tags
General Discussions
Asked by
Jesús
Top achievements
Rank 1
Answers by
Paul
Telerik team
Jesús
Top achievements
Rank 1
Share this question
or