User Control Javascript
am 17.01.2008 22:41:28 von ge0193387
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?
RE: User Control Javascript
am 17.01.2008 23:07:04 von chad
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" 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?
>
RE: User Control Javascript
am 18.01.2008 20:49:02 von mily242
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.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" 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?
> >