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

How do Telerik controls add their embedded scripts?

2 Answers 85 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 27 Apr 2016, 07:35 PM

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">
<Scripts>
<asp:ScriptReference Assembly="Core.WebBase" Name="Core.Resources.Scripts.ScriptTest.js" />
</Scripts>
</asp:ScriptManager>

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 Answers, 1 is accepted

Sort by
0
Jonathan
Top achievements
Rank 1
answered on 27 Apr 2016, 08:27 PM

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.

0
Vessy
Telerik team
answered on 28 Apr 2016, 05:00 AM
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:
https://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerclientscriptresource%28v=vs.110%29.aspx

Regards,
Vessy
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
General Discussions
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Jonathan
Top achievements
Rank 1
Vessy
Telerik team
Share this question
or