User Control Javascript

Discussion in 'ASP .Net' started by cfps.Christian, Jan 17, 2008.

  1. 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?
     
    cfps.Christian, Jan 17, 2008
    #1
    1. Advertising

  2. cfps.Christian

    Chad Scharf Guest

    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?
    >
     
    Chad Scharf, Jan 17, 2008
    #2
    1. Advertising

  3. 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.
     
    cfps.Christian, Jan 18, 2008
    #3
  4. 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?
    > >
     
    Milosz Skalecki [MCAD], Jan 18, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Rowton
    Replies:
    2
    Views:
    672
    Tom Rowton
    Aug 1, 2003
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    646
    Mad Scientist Jr
    Mar 22, 2006
  3. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    558
    Jonathan Wood
    Feb 2, 2010
  4. Faizan Ahmed
    Replies:
    1
    Views:
    289
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    366
    John Saunders
    Jan 4, 2005
Loading...

Share This Page