RegisterClientScriptInclude

Gibou

Active member
Joined
Mar 4, 2009
Messages
25
Location
Paris
Programming Experience
5-10
Hi,

I want to dynamically insert a <script type="text/javascript" src="..."></script> into my <head></head> markups.

I've seen I have to use the method RegisterClientScriptInclude.

I'm in .net 2.0 in this project and this my code:

VB.NET:
' Include some javascript external files
Dim jScript As ClientScriptManager = Page.ClientScript
jScript.RegisterClientScriptInclude(Me.GetType, "CoreApplication.js", jScript.GetWebResourceUrl(Me.GetType, Session("root") & "js/CoreApplication.js"))
jScript.RegisterClientScriptInclude(Me.GetType, "search.js", jScript.GetWebResourceUrl(Me.GetType, Session("root") & "js/search.js"))

I've tested different solutions:
  • With the GetWebResourceUrl
  • Without the GetWebResourceUrl
  • With the first parameter (type)
  • Without the first parameter

THe problem is that the script pages are not included.
This code is in my masterpage code behind. Those pages are useful in all pages which use this masterpage.
I've tried to insert this code in a content page, it does not work either.
The code is actually in a method which handles the Me.Load event.

If you have some solutions

thanks by advance :)

PS: I've already read the msdn documentation and some other websites on this subject.
 
This is how i add script to the head section.

<head runat="server">
<title>My Title</title>
<asp:Literal ID="litScripts" runat="server"></asp:Literal>
</head>

VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      litScripts.Text = "<script src=""js/myScript.js"" type=""text/javascript""></script>" & Environment.NewLine 
End Sub
 
Indeed, why not.
However, I'm a little bit curious. If .net has this function, it must work so what is wrong with my code ? It's that I want to understand.

Until I found my mistake, I keep your solution, thanks, but I'm still looking for an explanation :)
 
Hello Gibou,

Please try this:

VB.NET:
Page.ClientScript.RegisterClientScriptInclude("nothing", Session("root") & "/" & ResolveClientUrl("js/search.js"))

This should work!

Btw, I am not 100% sure about the GetWebResourceUrl method so i would use ResolveClientUrl method, so your paths are correct on the pages that are being loaded.

Even you could set it declaratively. Just add this tag in the header <head> ... </head>

VB.NET:
<script language="javascript" src='<%= Session("root") & "/" & ResolveClientUrl("js/search.js")%>'></script>

Hope this helps!
 
Back
Top