Using js file in asp.net 2.0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    #1

    Using js file in asp.net 2.0

    Hi,

    I simply wish to code a JS function ONCE, in a .js file, include the .js
    file in the Master page, and then in several but not all pages, I wish to
    execute the function on pageload.

    I don't want to use the
    Page.ClientScri pt.RegisterStar tupScript(this. GetType(),"disp laymessage",
    strScript, false);

    method because this seemingly requires me to code the JScript on all the
    pages (code behind) where I want to run the function, which is stupid.



    I think what I want to use is something like :



    Designate the body tag as :

    <body runat="server" id="body">

    and in the code behind for a particular page:

    protected void Page_Load(objec t sender, EventArgs e)

    {

    try

    {

    string temp = "displaymessage ();";


    HtmlGenericCont rol body =
    (HtmlGenericCon trol)Master.Fin dControl("Body" );

    body.Attributes .Add("onload", temp);

    }

    catch (NullReferenceE xception x)

    {

    Response.Write( x);

    }

    }



    but this give a JS error where it says 'Object Required' where the body tag
    is in the rendered html



    It does work if I do:

    protected void Page_Load(objec t sender, EventArgs e)

    {

    try

    {

    string temp = "alert('Hel lo World!');";

    HtmlGenericCont rol body =
    (HtmlGenericCon trol)Master.Fin dControl("Body" );

    body.Attributes .Add("onload", temp);

    }

    catch (NullReferenceE xception x)

    {

    Response.Write( x);

    }

    }



    So what is wrong?


  • Michael Nemtsev

    #2
    Re: Using js file in asp.net 2.0

    Hello Frank,

    Have u tried to use RegisterClientS criptInclude ?

    FI simply wish to code a JS function ONCE, in a .js file, include the
    F.js file in the Master page, and then in several but not all pages, I
    Fwish to execute the function on pageload.
    F>
    FI don't want to use the
    FPage.ClientScr ipt.RegisterSta rtupScript(this .GetType(),"dis playmessag
    Fe", strScript, false);
    F>
    Fmethod because this seemingly requires me to code the JScript on all
    Fthe pages (code behind) where I want to run the function, which is
    Fstupid.

    ---
    WBR,
    Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

    "At times one remains faithful to a cause only because its opponents do not
    cease to be insipid." (c) Friedrich Nietzsche


    Comment

    • Frank

      #3
      Re: Using js file in asp.net 2.0

      Thanks for your repsonse. I have tried this:

      Created a file name global.js which contains:
      // JScript File

      function displaymessage( )

      {

      alert("Hello World!")

      }

      Then in the Master page I did like so:

      <head runat="server">



      <script language="JavaS cript" src="common/js/global.js"
      type="text/JavaScript"></script>

      </head>

      <body runat="server" id="body"



      Then in one of the pages in the site I did like so:

      protected void Page_Load(objec t sender, EventArgs e)

      {

      // Define the name, type and url of the client script on the page.

      String csname = "PageLoadScript ";

      String csurl = "~/common/gjs/global.js";

      Type cstype = this.GetType();

      // Get a ClientScriptMan ager reference from the Page class.

      ClientScriptMan ager cs = Page.ClientScri pt;

      // Check to see if the include script exists already.

      if (!cs.IsClientSc riptIncludeRegi stered(cstype, csname))

      {

      cs.RegisterClie ntScriptInclude (cstype, csname, ResolveClientUr l(csurl));

      }

      string temp = "displaymessage ()";

      HtmlGenericCont rol body = (HtmlGenericCon trol)Master.Fin dControl("Body" );

      body.Attributes .Add("onload", temp);

      }



      The page starts up but a JS error occurs (Object Expected) and point to the
      body tag line... The rendered HTML starts like so:



      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head><title>
      Untitled Page
      </title><link href="../common/css/admin_style.css " rel="stylesheet "
      type="text/css" />
      <script language="JavaS cript" src="common/js/global.js"
      type="text/JavaScript"></script>
      </head>
      <body id="ctl00_body " onload="display message()">
      <form name="aspnetFor m" method="post" action="test.as px"
      id="aspnetForm" >
      <div>.

      ..

      ..



      "Michael Nemtsev" <nemtsev@msn.co mwrote in message
      news:1799a79b3c 72a78c8e863acc0 c87a@msnews.mic rosoft.com...
      Hello Frank,
      >
      Have u tried to use RegisterClientS criptInclude ?
      >
      FI simply wish to code a JS function ONCE, in a .js file, include the
      F.js file in the Master page, and then in several but not all pages, I
      Fwish to execute the function on pageload.
      FFI don't want to use the
      FPage.ClientScr ipt.RegisterSta rtupScript(this .GetType(),"dis playmessag
      Fe", strScript, false);
      FFmethod because this seemingly requires me to code the JScript on all
      Fthe pages (code behind) where I want to run the function, which is
      Fstupid.
      >
      ---
      WBR,
      Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour
      >
      "At times one remains faithful to a cause only because its opponents do
      not cease to be insipid." (c) Friedrich Nietzsche
      >
      >

      Comment

      • Frank

        #4
        Re: Using js file in asp.net 2.0

        OK, I got it:

        <%@ Page Language="C#"%>

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">


        public void Page_Load(Objec t sender, EventArgs e)
        {
        // Define the name, type and url of the client script on the page.
        String csname = "ButtonClickScr ipt";
        String csurl = "~/script_include. js";
        Type cstype = this.GetType();

        // Get a ClientScriptMan ager reference from the Page class.
        ClientScriptMan ager cs = Page.ClientScri pt;

        // Check to see if the include script exists already.
        if (!cs.IsClientSc riptIncludeRegi stered(cstype, csname))
        {
        cs.RegisterClie ntScriptInclude (cstype, csname,
        ResolveClientUr l(csurl));
        }

        //for the body onload functionality
        string temp = "displaymessage ()";
        HtmlGenericCont rol body =
        (HtmlGenericCon trol)Master.Fin dControl("Body" );
        body.Attributes .Add("onload", temp);

        }


        <html >
        <head>
        <title>ClientSc riptManager Example</title>
        </head>
        <body>
        <form id="Form1" runat="server">
        <div>
        <input type="text"
        id="Message"/>
        <input type="button"
        value="ClickMe"
        onclick="DoClic k()"/>
        </div>
        </form>
        </body>
        </html>

        This example requires a JavaScript file named Script_include. js with the
        following contents:


        function DoClick() {Form1.Message. value='Text from include script.'}


        function displaymessage( )
        {
        alert('Hello World!');
        }


        *** add <body runat="server" id="body"to masterpage for the pageload
        functionality


        Comment

        Working...