Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > User Control Javascript

Reply
Thread Tools

User Control Javascript

 
 
cfps.Christian
Guest
Posts: n/a
 
      01-17-2008
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?
 
Reply With Quote
 
 
 
 
Chad Scharf
Guest
Posts: n/a
 
      01-17-2008
Try something like this:

In your AssemblyInfo.cs file, place the following:

[assembly: System.Web.UI.WebResource("AssemblyName.filepath.F ileName.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" wrote:

> 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?
>

 
Reply With Quote
 
 
 
 
cfps.Christian
Guest
Posts: n/a
 
      01-18-2008
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.
 
Reply With Quote
 
Milosz Skalecki [MCAD]
Guest
Posts: n/a
 
      01-18-2008
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" wrote:

> Try something like this:
>
> In your AssemblyInfo.cs file, place the following:
>
> [assembly: System.Web.UI.WebResource("AssemblyName.filepath.F ileName.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" wrote:
>
> > 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?
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
using javascript in User controls to access server controls of the user control Faizan Ahmed ASP .Net Building Controls 1 01-04-2005 04:09 PM
using javascript in User controls to access server controls of the user control Faizan Ahmed ASP .Net Web Controls 1 01-04-2005 04:09 PM
Manipulating a User Control from another User Control Tom Rowton ASP .Net 2 08-01-2003 08:18 PM



Advertisments