User Control Javascript

C

cfps.Christian

I'm trying to have a javascript file included with my usercontrol, but
when the page loads it says that none of my JS methods are there.
What are the steps that I need to take to get a javascript file to go
with the user control so that I don't have to load the script onto the
page that is calling the control?
 
C

Chad Scharf

Try something like this:

In your AssemblyInfo.cs file, place the following:

[assembly: System.Web.UI.WebResource("AssemblyName.filepath.FileName.js",
"text/javascript")]

where AssemblyName is, for instance, MyNamespace.dll, so "MyNamespace", then
each folder nested within your assembly, e.g. in my project I have a folder
called "scripts", so filepath above would be "scripts", the result would be
"MyNamespace.scripts.MyFile.js".

Ensure in the file properties within Solution Explorer in VS that the Build
Type is "Embedded Resource" for the script file itself.

Finally, override the OnPreRender event in your control, add the following
(if you are going to be using a ScriptManager control/AJAX, otherwise use
Page.ClientScript... where you see ScriptManager:

// Register JavaScript
string jsPath = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Retina.Web.UI.Controls.FloatingMenu.FloatingMenu.js");
ScriptManager sm = ScriptManager.GetCurrent(this.Page);
ScriptReference sr = new ScriptReference(jsPath);
if (!sm.Scripts.Contains(sr))
sm.Scripts.Add(sr);


Hope that helps. Let me know if you have any questions or if anyone has a
more efficient way of doing this by all means...
 
C

cfps.Christian

Well the first part of my problem is that I don't have the
AssemblyInfo.cs since I unfortunately have to code this in VB. Even
then I'm not sure how to make it appear because I know there is an
assembly info hiding somewhere in my project.
 
M

Milosz Skalecki [MCAD]

You can't do it in Web Site project. I've been trying to solve the same
problem, but it seems you have to create separate class library with
resources embedded as you said and then merged with ILMerge/aspnet_merge.
Haven't tested it yet.

Regards
--
Milosz


Chad Scharf said:
Try something like this:

In your AssemblyInfo.cs file, place the following:

[assembly: System.Web.UI.WebResource("AssemblyName.filepath.FileName.js",
"text/javascript")]

where AssemblyName is, for instance, MyNamespace.dll, so "MyNamespace", then
each folder nested within your assembly, e.g. in my project I have a folder
called "scripts", so filepath above would be "scripts", the result would be
"MyNamespace.scripts.MyFile.js".

Ensure in the file properties within Solution Explorer in VS that the Build
Type is "Embedded Resource" for the script file itself.

Finally, override the OnPreRender event in your control, add the following
(if you are going to be using a ScriptManager control/AJAX, otherwise use
Page.ClientScript... where you see ScriptManager:

// Register JavaScript
string jsPath = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Retina.Web.UI.Controls.FloatingMenu.FloatingMenu.js");
ScriptManager sm = ScriptManager.GetCurrent(this.Page);
ScriptReference sr = new ScriptReference(jsPath);
if (!sm.Scripts.Contains(sr))
sm.Scripts.Add(sr);


Hope that helps. Let me know if you have any questions or if anyone has a
more efficient way of doing this by all means...

--
Chad Scharf
_______________________________
http://www.chadscharf.com


cfps.Christian said:
I'm trying to have a javascript file included with my usercontrol, but
when the page loads it says that none of my JS methods are there.
What are the steps that I need to take to get a javascript file to go
with the user control so that I don't have to load the script onto the
page that is calling the control?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top