How do Telerik controls add their embedded scripts?

3 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    12 posts
    Member since:
    Aug 2015

    Posted 27 Apr Link to this post

    How do Telerik controls programmatically register their scripts with the ScriptManager? I have created a custom control and the only way I can get the scripts to register is by explicitly referencing the script in the page's markup.

    <asp:ScriptManager runat="server" ID="ScriptManager1">
    <asp:ScriptReference Assembly="Core.WebBase" Name="Core.Resources.Scripts.ScriptTest.js" />

    But how to do this programmatically? I cannot expect my users to know to do this for every page they use the control on. I've used the ClientScript to do this like so: Page.ClientScript.RegisterClientScriptResource(typeof(ScriptTest), "Atl.Core.Resources.Scripts.ScriptTest.js"); but this does not work with asynchronous postbacks. Whats the trick? :)

  2. Jonathan
    Jonathan avatar
    12 posts
    Member since:
    Aug 2015

    Posted 27 Apr in reply to Jonathan Link to this post

    Well, I figured it out. It is as simple as using

    ScriptManager.RegisterClientScriptResource(this, typeof(ScriptTest), "Core.Resources.Scripts.ScriptTest.js");

    instead of

    Page.ClientScript.RegisterClientScriptResource(typeof(ScriptTest), "Core.Resources.Scripts.ScriptTest.js");

    I was confused because I didn't realize RegisterClientScriptResource is a static method on the ScriptManager class. I thought I had to get a reference of the ScriptManager from the Page.

  3. DevCraft R3 2016 release webinar banner
  4. Vessy
    Vessy avatar
    1353 posts

    Posted 27 Apr Link to this post

    Hi Jonathan,

    I am glad to see that you have managed to find the solution on your own - yes, we are also registering our scripts as ClientScriptResources of the ScriptManager.

    You can also find useful the following MSDN article explaining the usage of the RegisterClientScriptResource method:

    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top