Substitution in embedded javascript

Last post 06-07-2007 11:41 AM by jwscuba. 2 replies.

Sort Posts:

  • Substitution in embedded javascript

    05-29-2007, 10:56 AM
    • Loading...
    • Blunder
    • Joined on 05-29-2007, 2:24 PM
    • Aylesbury, UK
    • Posts 1

    I have a custom server control that has an embedded js file that is embedded with:
    this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "a_useful_name", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "assembly.javascriptfilename.js"));

    OR
    this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "assembly.javascriptfilename.js");

    the assemblyinfo.cs has this line:
    [assembly: System.Web.UI.WebResource("assembly.javascriptfilename.js", "text/javascript", PerformSubstitution=true)]

    In an aspx file I would use '<%= m_callbackInvocation %>' in my javascript to get the value in the code behind. So in my js file I have: var measureVectorCallbackFunctionString = '<%=m_callbackInvocation%>';

    Unfortunately, the substitution doesn't work as I fear that it is for things like <%= WebResource(....) %>.

     Is there any way that I can callback into the server control code from the js file?

    Thanks.

  • Re: Substitution in embedded javascript

    06-05-2007, 12:25 AM
    Answer

    Hi,

    You can refer the following way to call the js file in your server control.

    Suppose we have two projects in Visual Studio. One is WebSite project (WebSite1) and another is WebControl Project (WebControlLibrary1). The first project includes 'Default.aspx' while the second includes 'ClientScriptResourceLabel.cs'. And we have a js file called 'script_include.js'.

    Step1. In the property window of 'script_include.js'. Assign the Build Action attribute with 'Embedded Resource'.

    Step2. Add the following code in CilentScriptResourceLabel.cs.

    [assembly: WebResource("script_include.js", "application/x-javascript")] 
    namespace WebControlLibrary1 
    {
       ...
    }
    Step3. Until now, We'll see WebControlLibrary1.script_include.js in the resource folder of WebControlLibrary but actually we want the js file attached to the resource folder without the namespace. So you can open WebControlLibrary1.csproj to make the <RootNameSpace> node to be empty.(Only supported in VS2005 Beta2)
    Step4. Finnaly, Invoke the resource in cs file.
     
    public class ClientScriptResourceLabel : WebControl 
        { 
            protected override void OnPreRender(EventArgs e) 
            { 
                if (this.Page != null) 
                { 
                    ClientScriptManager manager1 = this.Page.ClientScript; 
                    manager1.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "script_include.js"); 
                } 
                base.OnPreRender(e); 
            } 
        } 
     
     Thanks.
    Sincerely,
    Michael Jin.
    Microsoft Online Community Support

  • Re: Substitution in embedded javascript

    06-07-2007, 11:41 AM
    • Loading...
    • jwscuba
    • Joined on 09-28-2004, 3:23 PM
    • Posts 85

    is there any way at all that we can get the script to be in the <head></head> tags.  When I use this method it seems to place it in the body after the form tag.

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter