Adding scripts with RadScriptManager

4 posts, 0 answers
  1. Ian
    Ian avatar
    129 posts
    Member since:
    Jun 2009

    Posted 17 May 2012 Link to this post

    Hi, I’m updating an ASP.NET AJAX application so that the jQuery library loaded by the Telerik.Web.UI assembly can be re-used by other scripts. I have two questions which I’d appreciate some advice on:

    I have a RadScriptManager on my master page and need to include a private javascript file that can be used by all content pages. Should I include the script as a reference within the RadScriptManager or should I use conventional script tags after the RadScriptManager? I’ve illustrated both methods below. Both seem to work but what is the recommended approach?

    METHOD 1
    <telerik:RadScriptManager ID="rsmMain" runat="server" >
    <Scripts>
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
    </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript" src="../Scripts/Utility.js"></script>

    METHOD 2
    <telerik:RadScriptManager ID="rsmMain" runat="server" >
    <Scripts>
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
    <asp:ScriptReference Path="~/Scripts/Utility.js" />
    </Scripts>
    </telerik:RadScriptManager>

    My second question relates to adding a client-side script from a page programmatically. Previously I used the RegisterStartupScript() method of ClientScriptManager to add a script from code behind but I note from some forum posts that it’s suggested you should use RadScriptManager.RegisterStartupScript() instead. What’s the difference and is there a recommended approach?

    Regards, Ian
  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 21 May 2012 Link to this post

    Hi Ian,

    With respect to your first question: it doesn't really matter how you register the scripts. You may chose whatever approach you like better.

    Regarding the second question, RegisterStartupScript on the script manager, it registers a startup script block for a control that is inside an UpdatePanel by using the ScriptManager control, and adds the script block to the page. In contrast, ClientScript.RegisterStartupScripts register a startup scripts with the page, it does not work with Ajax.

    Kind regards,
    Genady Sergeev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ian
    Ian avatar
    129 posts
    Member since:
    Jun 2009

    Posted 21 May 2012 Link to this post

    Hi Genady,
    Thanks for the explanation - it sounds like I should be using RadScriptManager.RegisterStartupScript() to register my script now.

    One final question. When I previously used the ClientScriptManager method, there was a IsStartupScriptRegistered() method you needed to test to ensure the script [you wanted to add] wasn't already registered. There doesn't appear to be any equivalent with RadScriptManager. Does that mean RadScriptManager does this check internally?

    Best regards, Ian 
     
  5. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 24 May 2012 Link to this post

    Hi Ian,

    RadScriptManager.RegisterStarupScript is simply inherited from the ASPNET ScriptManager. I've looked at he ScriptManager source code and noticed that it simply uses ClientScript.RegisterStartupScript in order to register the script, however, it does not perform check whether the script is already registered. You should be able to use ClientScript.IsStartupScriptRegistered to check that.

    Regards,
    Genady Sergeev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top