RegisterClientScriptResource - Can't get it working - Namespace Problem?

Last post 02-16-2006 4:04 PM by Garbin. 3 replies.

Sort Posts:

  • RegisterClientScriptResource - Can't get it working - Namespace Problem?

    02-16-2006, 11:57 AM
    • Member
      102 point Member
    • brooklynDev
    • Member since 12-16-2005, 11:57 PM
    • Posts 27
    I'm trying to register some client scripts in my page, to no avail. My code looks like this:

    using System;
    ...

    [assembly: WebResource("Javascript.Global.js", "text/javascript")]
    public class BasePage : System.Web.UI.Page
    {
        public BasePage() : base()
        {}

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptResource(typeof(string),"Javascript.Global.js");

            base.OnPreRender(e);
        }
    }

    where BasePage.cs lives in '/App_Code/BasePage.cs' and Global.js lives in '/Javascript/Global.js'.

    Since pages and master pages are partial classes and can't live under a namespace directly I don't know how to reference the resource/javascript file like 'Namespace.Directories.FileName.js'. If the class (a page) I'm registering the script to doesn't have a namespace defined can I omit a namespace completely in my web resource definition and my register script method call? Am I doing something else wrong?

    bd
  • Re: RegisterClientScriptResource - Can't get it working - Namespace Problem?

    02-16-2006, 2:16 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 8:35 AM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    one approach that I use is to create a static class called WebResources, put it under the desired namespace and define the metadata attributes there:

    namespace MyNamespace {
       
        // Declare all the embedded resources here.
        [assembly: WebResource("MyNamespace.Javascript.Global.js", "text/javascript")]
        public static class WebResources {

        }

    }


    and then, to register an embedded resource, pass the type of  the static class and then the reference to the resource in the form Namespace.Directories.FileName:

    Page.ClientScript.RegisterClientScriptResource(typeof(MyNamespace.WebResources), "MyNamespace.Javascript.Global.js");

    Finally, remember to set the Build Action for the desired resource file to 'Embedded Resource' (this can be done in the Properties window for that file).
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: RegisterClientScriptResource - Can't get it working - Namespace Problem?

    02-16-2006, 2:47 PM
    • Member
      102 point Member
    • brooklynDev
    • Member since 12-16-2005, 11:57 PM
    • Posts 27
    how can you set the build action for a file in a web project?

    In other types of projects like class libraries I see the build action property in the properties window but for web applications I don't see that field in my properties window.

    When I click on the javascript file I want to embed and look at the properties window I don't see a 'Build Action' field.

    bd
  • Re: RegisterClientScriptResource - Can't get it working - Namespace Problem?

    02-16-2006, 4:04 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 8:35 AM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    well you're right, you have to move your resources to a separate project (of type class library) in order to be able to embed them in an assembly (sorry, I didn't realize that you were going to embed them in a website project).
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
Page 1 of 1 (4 items)