locked
migrate javascript to library RRS feed

  • Question

  • User304384516 posted

    I have some javascript in the <head> section of a master page. I want to move it to a server control project. What is the simplest way to do it? I can create a .js file containing the javascript, then add it ot the server control project as an embedded resource. The web site has a reference to the server control dll.

    Bill

    Wednesday, September 7, 2011 12:28 PM

Answers

  • User3866881 posted

    Hello,

    In Assembly.cs file, please add this:

    [assembly: WebResource("CustomControls.GridViewScript.js", "text/javascript")]

    And then in your OnInit, please do this:

    protected override void OnPreRender(EventArgs e)
    {
       
    base.OnPreRender(e);
        string
    resourceName = "CustomControls.GridViewScript.js";

       
    ClientScriptManager cs = this.Page.ClientScript;
       
    cs.RegisterClientScriptResource(typeof(CustomControls.CustomGridView), resourceName);
    }

    For more please see: http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2011 9:14 PM
  • User-742633084 posted

    Hi swartzbill,

    So based on your further description, actually you will want the script resource's url path at runtime so that you can dynamically bind it or assign it to some properties or markup on the page(from your control's code).

    I think Decker's original suggestion have made in the right direction since for ASP.NET custom server control( we do not have the entire control over the Page's rendering and output), we need to leverage the "Page.ClientScript"(the script manager) to register some script or reference into the page content.

    And instead of the RegisterClientscript method, you can use another method "GetWebResourceUrl". This method can help directly return the url path of a certain script(or other static resource file) which is emit from an embeded resource in control's assembly:

    #ClientScriptManager.GetWebResourceUrl Method
    http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx

    #Embedding Resources in ASP.NET 2.0 Assemblies
    http://aspalliance.com/726

    BTW, for debugging and tracing such kind of dynamically added script reference, I suggest you use fiddler or the built-in IE develpment toolbar to watch the network trace to see if the dynamic reference is correctly registered on the page(from your server control's code).

    #Discovering Internet Explorer Developer Tools
    http://msdn.microsoft.com/en-us/library/dd565628(VS.85).aspx

    #Fiddler2 - Installation Information
    http://www.fiddler2.com/fiddler2/version.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2011 10:42 PM
  • User304384516 posted

    Decker's reply certainly works. The following also works. In the head section of the master page:

      <script src='<%# Page.ClientScript.GetWebResourceUrl(typeof(ServerCtrlProjNameSpace.PickAnyClass), "ServerCtrlProjNameSpace.Scripts.dateValidation.js") %>' type="text/javascript"></script>

    The inline data-binding expression is triggered by the following in the load event handler for the master page:

    Page.Header.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 12, 2011 10:43 AM

All replies

  • User3866881 posted

    Hello,

    In Assembly.cs file, please add this:

    [assembly: WebResource("CustomControls.GridViewScript.js", "text/javascript")]

    And then in your OnInit, please do this:

    protected override void OnPreRender(EventArgs e)
    {
       
    base.OnPreRender(e);
        string
    resourceName = "CustomControls.GridViewScript.js";

       
    ClientScriptManager cs = this.Page.ClientScript;
       
    cs.RegisterClientScriptResource(typeof(CustomControls.CustomGridView), resourceName);
    }

    For more please see: http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2011 9:14 PM
  • User304384516 posted

    I can't get it to work. Here is what I tried.

    I added the .js file to the asp.net server control project as an embedded resource. I added the WebResourceAttribute line to AssemblyInfo.cs in the server control project.

    Since the original javascript was in the <head> part of Site.master, I added the following to Site.master.cs in the web site project.

      protected override void OnPreRender(EventArgs e)
      {
        base.OnPreRender(e);

        string resourceName = "TssbControls.Scripts.dateValidation.js";
        ClientScriptManager cs = this.Page.ClientScript;
        cs.RegisterClientScriptResource(this.GetType(), resourceName);
      }

    The js file has a jQuery ready handler in it. I added an alert('HELLO'). It doesn't fire.

    Bill

    Friday, September 9, 2011 5:18 PM
  • User304384516 posted

    It now works. I changed the type arg to RegisterClientScriptResource. I randomly picked a class in the server control project. What I really want to do is

    <script src='<%# ??????? %>' type="text/javascript" />

    where ???????? is triggered by a call to DataBind() in an event handler in the code behind for Site.master. Any ideas?

    Friday, September 9, 2011 6:00 PM
  • User-742633084 posted

    Hi swartzbill,

    So based on your further description, actually you will want the script resource's url path at runtime so that you can dynamically bind it or assign it to some properties or markup on the page(from your control's code).

    I think Decker's original suggestion have made in the right direction since for ASP.NET custom server control( we do not have the entire control over the Page's rendering and output), we need to leverage the "Page.ClientScript"(the script manager) to register some script or reference into the page content.

    And instead of the RegisterClientscript method, you can use another method "GetWebResourceUrl". This method can help directly return the url path of a certain script(or other static resource file) which is emit from an embeded resource in control's assembly:

    #ClientScriptManager.GetWebResourceUrl Method
    http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx

    #Embedding Resources in ASP.NET 2.0 Assemblies
    http://aspalliance.com/726

    BTW, for debugging and tracing such kind of dynamically added script reference, I suggest you use fiddler or the built-in IE develpment toolbar to watch the network trace to see if the dynamic reference is correctly registered on the page(from your server control's code).

    #Discovering Internet Explorer Developer Tools
    http://msdn.microsoft.com/en-us/library/dd565628(VS.85).aspx

    #Fiddler2 - Installation Information
    http://www.fiddler2.com/fiddler2/version.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2011 10:42 PM
  • User304384516 posted

    Decker's reply certainly works. The following also works. In the head section of the master page:

      <script src='<%# Page.ClientScript.GetWebResourceUrl(typeof(ServerCtrlProjNameSpace.PickAnyClass), "ServerCtrlProjNameSpace.Scripts.dateValidation.js") %>' type="text/javascript"></script>

    The inline data-binding expression is triggered by the following in the load event handler for the master page:

    Page.Header.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 12, 2011 10:43 AM