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

Script controls may not be registered after PreRender

12 Answers 988 Views
Dock
This is a migrated thread and some comments may be shown as answers.
CSurieux
Top achievements
Rank 2
CSurieux asked on 17 Mar 2008, 05:35 PM
Hello,

I am trying to use code similar to the raddock samples , fcreating dynamically raddocks and inserting them in raddockzones.

I use the same code for creating the docks

   protected void AddNewDock(ModuleItem m,RadDockZone zn )
  {
            Trace2.WriteLineIf(PageTraceSwitch.Sw.TraceVerbose, string.Format("[V]PageLayout AddNewDock module ID: {0}, zone.ID: {1}, zone.ClientID: {1}", m.ID,zn.ID,zn.ClientID));
   RadDock dock        =   CreateRadDock(m);
   UpdatePanel1.ContentTemplateContainer.Controls.Add(dock);
            string scrpt    =   string.Format(@"function _addDock() {{ Sys.Application.remove_load(_addDock); 
                        $find('{1}').dock($find('{0}')); 
                        $find('{0}').doPostBack('DockPositionChanged');}};
                        Sys.Application.add_load(_addDock);",dock.ClientID,zn.ClientID);
            Trace2.WriteLineIf(PageTraceSwitch.Sw.TraceVerbose, string.Format("[V]PageLayout AddNewDock script:\r\n {0}", scrpt));
            ScriptManager.RegisterStartupScript(dock,GetType(),"AddDock",scrpt,true);

   CreateSaveStateTrigger(dock);

   dock.Tag = m.ID.ToString();
  }
My aspx page is very similar but the raddocklayout in in the pageview of a Mutipage with a RadTabStrip.

            <radTS:PageView ID="Pageview2" Width=100% runat="server">
                <fieldset id="Fieldset2" class="SettingsTableGroup" runat="server">
                    <legend id="legend1" runat="server">
                        <asp:Literal ID="Literal1" Text="<%$ Resources:ResourcesWTCRM,PAGESETTINGS_LAYOUT %>"
                            runat="server"></asp:Literal>
                    </legend>
                    <table cellspacing="1" cellpadding="2" border="0" width=100%>
                        <tr>
                            <td>
                                <asp:Literal ID="addmodule" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_ADDMODULE %>"></asp:Literal></td>
                            <td>
                                <asp:Literal ID="module_type" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_MODULETYPE %>"></asp:Literal></td>
                            <td colspan="2">
                                <asp:DropDownList ID="moduleType" runat="server" CssClass="NormalTextBox" DataTextField="FriendlyName"
                                    DataValueField="ModuleDefID">
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <asp:Literal ID="moduleLocationLabel" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_MODULELOCATION %>"></asp:Literal></td>
                            <td valign="top" colspan="2">
                                <asp:DropDownList ID="paneLocation" runat="server">
                                    <asp:ListItem Value="LeftPane" Text="<%$ Resources:ResourcesWTCRM,AM_MODULELOCATION_LEFT %>"></asp:ListItem>
                                    <asp:ListItem Value="ContentPane" Selected="True" Text="<%$ Resources:ResourcesWTCRM,AM_MODULELOCATION_CENTER %>"></asp:ListItem>
                                    <asp:ListItem Value="RightPane" Text="<%$ Resources:ResourcesWTCRM,AM_MODULELOCATION_RIGHT %>"></asp:ListItem>
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td valign="top" class="Normal">
                                <asp:Literal ID="moduleVisibleLabel" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_MODULEVISIBLETO %>"></asp:Literal></td>
                            <td valign="top" colspan="2">
                                <asp:DropDownList ID="viewPermissions" runat="server">
                                    <asp:ListItem Value='<% Config.CRMAllUsers+";"%>' Selected="True" Text="<%$ Resources:ResourcesWTCRM,AM_MODULEVISIBLETO_ALLUSERS %>"></asp:ListItem>
                                    <asp:ListItem Value="Authenticated Users;" Text="<%$ Resources:ResourcesWTCRM,AM_MODULEVISIBLETO_AUTHENTICATED_USERS %>"></asp:ListItem>
                                    <asp:ListItem Value="System Administrator;" Text="<%$ Resources:ResourcesWTCRM,AM_MODULEVISIBLETO_ADMINSROLE %>"></asp:ListItem>
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                <asp:Literal ID="module_name" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_MODULENAME %>"></asp:Literal></td>
                            <td colspan="2">
                                <asp:TextBox ID="moduleTitle" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_NEWMODULENAME %>" CssClass="NormalTextBox"
                                    Width="250" EnableViewState="false"></asp:TextBox>
                                &nbsp;<asp:ImageButton ID="AddModuleBtn" SkinID="AddModule" runat="server" AlternateText="<%$ Resources:ResourcesWTCRM,AM_ADDMODULEBELOW %>" />
          </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <hr noshade=noshade size="1" />
                            </td>
                        </tr>
                        <tr valign="top">
                            <td colspan=3 >
                            <telerik:raddocklayout runat="server" id="RadDockLayout1">
                            <div style="width:660px;margin-left:auto;margin-right:auto" >
                                <telerik:raddockzone runat="server" id="RadDockZone1" width="30%" MinHeight="200" style="float:left;margin-right:5%;background: #f5f4e8;" />                                </telerik:raddockzone>
                                <telerik:raddockzone runat="server" id="RadDockZone2" width="30%" MinHeight="200" style="float:left;margin-right:5%;background: #d5f0fa;" />
                                <telerik:raddockzone runat="server" id="RadDockZone3" width="30%" MinHeight="200" style="float:left;background: #d5f0fa;" />
                            </div>
                            <div style="display:none">
                                <asp:updatepanel runat="server" id="UpdatePanel1" >
                                    <triggers>
                                        <asp:asyncpostbacktrigger controlid="AddModuleBtn" eventname="Click" />
                                    </triggers>
                                </asp:updatepanel>
                            </div>
                            </telerik:raddocklayout>
                            </td>
                        </tr>
                        <tr>
                            <td class="Error" align="center" colspan="4">
                                <asp:Literal ID="msgError" runat="server" Text="<%$ Resources:ResourcesWTCRM,AM_NO_RIGHTS %>"></asp:Literal>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </radTS:PageView>

This page is using a masterpage which contains a radscriptmanager
            <telerik:RadScriptManager ID="WTScriptManager" Runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true"  EnableTheming="True">
            </telerik:RadScriptManager>

the page itself hase a proxy as:
    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat=server />

Everything starts well, the page is displayed but as soon as displayed a postback is sent, certainly by the generated script, and I get the exception in the postback handling:

[V]Global Application_Error ex : System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
 System.InvalidOperationException: Script controls may not be registered after PreRender.
   at System.Web.UI.ScriptControlManager.RegisterScriptControl[TScriptControl](TScriptControl scriptControl)
   at System.Web.UI.ScriptManager.RegisterScriptControl[TScriptControl](TScriptControl scriptControl)
   at Telerik.Web.UI.RadWebControl.RegisterScriptControl()
   at Telerik.Web.UI.RadWebControl.OnPreRender(EventArgs e)
   at Telerik.Web.UI.RadInputControl.OnPreRender(EventArgs e)
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)



Where is the error ?
I can't understand why I get this full pstback and not an ajax call ?
Thanks for help.

CS

12 Answers, 1 is accepted

Sort by
0
CSurieux
Top achievements
Rank 2
answered on 21 Mar 2008, 02:25 PM
First thanks for answer.
Next I have checked with a Trace through all code and all controls are created before PreRender.
The page itself has an override for PreRender and it takes care to call base.PreRender.

What I read in my traces is that it is a telerik RadInputControl which triggers some event as written in:
at Telerik.Web.UI.RadInputControl.OnPreRender(EventArgs e)

Are you sure thta all telerik controls call base.PreRender ? Prometheus or not (as I must mix because RadTabStrip not being prted at that time).


I have not used reflector, may be uncessary as it is easier to ask you :)

Regards
0
Nikita Gourme
Top achievements
Rank 1
answered on 24 Mar 2008, 07:02 AM
My assumption is that all Telerik controls (Prometheus or not) call base.PreRender since they are server controls and their lifecycle should mirror the standard ASP.NET server control lifecycle.

Nikita
0
Cristi71000
Top achievements
Rank 1
answered on 21 Apr 2008, 06:58 AM
Hi,

I get the same error, but on a postback from a RadTreeView. This happens only on the postback. Took care of calling base.OnPreRender, out of ideas now...

C
0
CSurieux
Top achievements
Rank 2
answered on 21 Apr 2008, 04:44 PM
On my side, I am still blocked with this error, I just verified that when I don't uses RadInput controls, I don't get the error.
have you progressed on your side ?

Regards
CS
0
Simon
Telerik team
answered on 22 Apr 2008, 08:18 AM
Hi Cristi71000,

Is it possible that you are creating and using static events in your application?

If this is the case, please read the following blog post, which discusses the problem and its possible solutions.

Regards,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Cristi71000
Top achievements
Rank 1
answered on 22 Apr 2008, 08:27 AM
Hi Simon,

I don't have static events. I have a static object that holds controls that are displayed in the page. The controls are not static, they're just attributes of the object that is being rendered in the page.

The first time a control is rendered, all is ok. The second time, I get a crash. This only happens on the new (formerly prometheus) controls, the old ones didn't have the issue. I really need to get this solved ASAP.

Thanks,
Cristian
0
Cristi71000
Top achievements
Rank 1
answered on 22 Apr 2008, 10:08 AM
I managed to make things work using a trick/workaround which I'm not happy with and which I am not sure if will work in the long run. Every time I need to display a control which I have stored in my Session object (long story) I need to create a new one with the same properties, set the reference from the old one to the new and display the new one. This seems to work just fine, but it's obviously a drag...
0
CSurieux
Top achievements
Rank 2
answered on 23 May 2008, 08:01 PM
Hello Simon,

I finally caught the bug.
Cache was the problem.
I have a dictionary made with objects, each object contains a control element, this control element is instanciated anj added to a Page only when necessary when I want to edit the dictionary content.
I put this dictionary in cache to improve overall speed.
I am using RadInputs and RadComboBox controls and it appears that
the control elements, when saving the dictionary in cache,  are also saved
as members of the main dictionary object.
Doing so, depending on their status at the time of caching, they seem to
save an indicator saying that Prerender as already been done and when I
reload from cache and try to display again theses controls, they are unable
to register their script because the indicator says that:
'Script controls may not be registered after PreRender.'

This message is an error in itself, just use reflector and you will see that it is not generated for the reason it invoques.

Elempents in cache seems to have their value updated in cache (still not understood why ?) because I save the dictionary in cache as soon as it is first created and, at that step of the process, the controls are not created ????
Is it possible that some object in cache continue to be updated until it is disposed ? I was thinking thta they were serialised and stored on a special area, even on disk ?
So setting the value of controls to null after first read from cache solved this annoying issue.
Is'nt it possible to check automatically that a control has already been through a full cycle and allow it to register a second time ?

Thanks for any previous help.
0
Simon
Telerik team
answered on 29 May 2008, 11:52 AM
Hello there!

@Cristi71000:
    Actually, using static objects is pretty much the same case as using static events, so the problem should be resolved in a similar manner.

@Christian Surieux:
    Generally, server controls should not be cached; what is more, they should be created and destroyed during one page life cycle.
    You could find a detailed article dwelling on the matter here: ASP.NET Memory: Thou shalt not store UI objects in cache or session scope.


Regards,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
CSurieux
Top achievements
Rank 2
answered on 29 May 2008, 02:14 PM
Hi Simon,

Very interesting link.
I had not time to explore all the windows debuging tools delivered by technet, thanks to remind me that they could be usefull.

Concerning cache, the problem I was facing is that at the instant when I store in the cache, no controls are connected to the objects in the dictionnary.
I was not thinking cache was keeping directly the object, having some experience of cache engine that were storing the structure+datas (OODBMS) in suche a way tha when you restore yo have a clone object.
Pb with IIS cache is that the object continue to be changed even after storage, so even after full page cycle, they can't be garbage collected...and when you ask cache for the object...you get the controls...
I think this is a very poor implementation of cache but will have to deal with it.
A fast solution is to reset any controls from objects readen from cache, a better one should be an interface to ache that clone the object keeping only data and structure before storing in cache...not so easy to develop.

My be telerik could provide such an interface with its objects...

Anyway thanks for help.

CS
0
Simon
Telerik team
answered on 30 May 2008, 11:02 AM
Hi Christian Surieux,

I understand that this feature could be useful in some cases, but for now, we do not plan to implement it in our controls.

Anyway, you could store a control's properties (if you want to preserve the state) and its data in the cache. For example, with our controls, you could use GetXml()/LoadXml() methods to set/get the whole state of a control to/from the cache.

Once again, a better way of 'caching' a control is to cache only relevant data, not the object itself.

Sincerely yours,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Kevon
Top achievements
Rank 1
answered on 16 Nov 2010, 04:09 PM
If you have a custom skin you're using, adding it's CSS to the page using the Page_Init event via HtmlLink object does the trick... at least in my case.
Tags
Dock
Asked by
CSurieux
Top achievements
Rank 2
Answers by
CSurieux
Top achievements
Rank 2
Nikita Gourme
Top achievements
Rank 1
Cristi71000
Top achievements
Rank 1
Simon
Telerik team
Kevon
Top achievements
Rank 1
Share this question
or