JavaScript

Discussion in 'ASP .Net' started by Peter, Jan 17, 2006.

  1. Peter

    Peter Guest

    I have the following code that executes JavaScript, is there a way I can put the same JavaScript code in it's own file and execute the code in that file instead of having this JavaScript code in my C# code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    {
    this.Page.RegisterClientScriptBlock(
    "LetterScript",
    "<script language=javascript>" +
    "function CreateLetter(){" +
    "var word = new ActiveXObject(\"Word.Application\");" +
    "word.Visible = true; " +
    "var file = document.getElementById('txtTemplate').value; " +
    "var objDoc = word.Documents.Add(file); " +
    "objDoc.FormFields(\"FirstName\").Result = document.getElementById('txtFirstName').value; " +
    "objDoc.FormFields(\"LastName\").Result = document.getElementById('txtLastName').value; " +
    "} " +
    "self.focus(); " +
    "</script>");
    }

    this.Button1.Attributes.Add("onClick", "CreateLetter()");

    }


    Thanks


    Peter
    Peter, Jan 17, 2006
    #1
    1. Advertising

  2. If everything in your script is static, you may put it in a .js file (say
    "myscript.js") and have a <script> tag within your <head> portion of ASPX
    file.

    <html>
    <head>
    ...
    <script language="javascript" type="text/javascript"
    src="myscript.js">
    ...
    </head>
    <body>
    ...
    </body>
    </html>

    P.S.: Next time, only post question in plain text format, not in RTF one. It
    cause trouble for me that I can't copy & paste the code from VS.NET IDE to
    news client. I have to type them by hand, and it'll more prone to typing
    mistake.

    "Peter" <>
    ???????:...
    I have the following code that executes JavaScript, is there a way I can put
    the same JavaScript code in it's own file and execute the code in that file
    instead of having this JavaScript code in my C# code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    {
    this.Page.RegisterClientScriptBlock(
    "LetterScript",
    "<script language=javascript>" +
    "function CreateLetter(){" +
    "var word = new ActiveXObject(\"Word.Application\");" +
    "word.Visible = true; " +
    "var file = document.getElementById('txtTemplate').value; " +
    "var objDoc = word.Documents.Add(file); " +
    "objDoc.FormFields(\"FirstName\").Result =
    document.getElementById('txtFirstName').value; " +
    "objDoc.FormFields(\"LastName\").Result =
    document.getElementById('txtLastName').value; " +
    "} " +
    "self.focus(); " +
    "</script>");
    }

    this.Button1.Attributes.Add("onClick", "CreateLetter()");

    }


    Thanks


    Peter
    Lau Lei Cheong, Jan 17, 2006
    #2
    1. Advertising

  3. Hi Peter,

    Welcome.
    As for separate script code from the main page's code, I think you can
    consider put those script code into a txt or .js file and embeded the file
    as an embeded resource in assembly. Are you using asp.net 1.1 or 2.0 ? In
    1.1 since vs.net compiled all code into a main assembly, you can embed the
    script code text file into the main assembly... In 2.0 , since all pages
    code are dynamic compiled , you can consider create a class library and
    embed the text file in that class library assembly's resource....
    Then, we can read the text stream(script code) from the embeded resource in
    assemly at runtime. Here is a kb article mentioned on this:

    #How to embed and access resources by using Visual C#
    http://support.microsoft.com/kb/319292/en-us

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    --------------------
    | From: "Peter" <>
    | Subject: JavaScript
    | Date: Tue, 17 Jan 2006 00:11:28 -0600
    | Lines: 104
    | MIME-Version: 1.0
    | Content-Type: multipart/alternative;
    | boundary="----=_NextPart_000_0008_01C61AFA.9003C470"
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: cpe-69-23-74-9.new.res.rr.com 69.23.74.9
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:371325
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | I have the following code that executes JavaScript, is there a way I can
    put the same JavaScript code in it's own file and execute the code in that
    file instead of having this JavaScript code in my C# code?
    | private void Page_Load(object sender, System.EventArgs e)
    | {
    | if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    | {
    | this.Page.RegisterClientScriptBlock(
    | "LetterScript",
    | "<script language=javascript>" +
    | "function CreateLetter(){" +
    | "var word = new ActiveXObject(\"Word.Application\");" +
    | "word.Visible = true; " +
    | "var file = document.getElementById('txtTemplate').value; " +
    | "var objDoc = word.Documents.Add(file); " +
    | "objDoc.FormFields(\"FirstName\").Result =
    document.getElementById('txtFirstName').value; " +
    | "objDoc.FormFields(\"LastName\").Result =
    document.getElementById('txtLastName').value; " +
    | "} " +
    | "self.focus(); " +
    | "</script>");
    | }
    |
    | this.Button1.Attributes.Add("onClick", "CreateLetter()");
    | }
    | Thanks
    | Peter
    |
    Steven Cheng[MSFT], Jan 17, 2006
    #3
  4. Peter,

    Make a file LetterScript.js with function CreateLetter()... inside. Put a line
    <script src="LetterScript.js"></script> in the <head> part of your ..aspx file. That's it.

    Eliyahu

    "Peter" <> wrote in message news:...
    I have the following code that executes JavaScript, is there a way I can put the same JavaScript code in it's own file and execute the code in that file instead of having this JavaScript code in my C# code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    {
    this.Page.RegisterClientScriptBlock(
    "LetterScript",
    "<script language=javascript>" +
    "function CreateLetter(){" +
    "var word = new ActiveXObject(\"Word.Application\");" +
    "word.Visible = true; " +
    "var file = document.getElementById('txtTemplate').value; " +
    "var objDoc = word.Documents.Add(file); " +
    "objDoc.FormFields(\"FirstName\").Result = document.getElementById('txtFirstName').value; " +
    "objDoc.FormFields(\"LastName\").Result = document.getElementById('txtLastName').value; " +
    "} " +
    "self.focus(); " +
    "</script>");
    }

    this.Button1.Attributes.Add("onClick", "CreateLetter()");

    }


    Thanks


    Peter
    Eliyahu Goldin, Jan 17, 2006
    #4
  5. The code you posted does not execute JavaScript. It writes it. It adds it to
    the HTML of an HTML document that is returned to the client via your ASP.Net
    Page.

    The purpose of being able to dynamically add JavaScript to a Page is to
    dynamically or conditionally add the JavaScript to the Page, or to modify
    the JavaScript as you are putting it in the Page.

    If the JavaScript should *always* be in the Page, and should not be changed
    at any time, you can put it into an external .js file, or simply put it into
    the HTML of the Page Template.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    "Peter" <> wrote in message
    news:...
    I have the following code that executes JavaScript, is there a way I can put
    the same JavaScript code in it's own file and execute the code in that file
    instead of having this JavaScript code in my C# code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    {
    this.Page.RegisterClientScriptBlock(
    "LetterScript",
    "<script language=javascript>" +
    "function CreateLetter(){" +
    "var word = new ActiveXObject(\"Word.Application\");" +
    "word.Visible = true; " +
    "var file = document.getElementById('txtTemplate').value; " +
    "var objDoc = word.Documents.Add(file); " +
    "objDoc.FormFields(\"FirstName\").Result =
    document.getElementById('txtFirstName').value; " +
    "objDoc.FormFields(\"LastName\").Result =
    document.getElementById('txtLastName').value; " +
    "} " +
    "self.focus(); " +
    "</script>");
    }

    this.Button1.Attributes.Add("onClick", "CreateLetter()");

    }


    Thanks


    Peter
    Kevin Spencer, Jan 17, 2006
    #5
  6. Peter

    Peter Guest

    Thank you very much, this is what I was looking for!

    "Eliyahu Goldin" <> wrote in message news:...
    Peter,

    Make a file LetterScript.js with function CreateLetter()... inside. Put a line
    <script src="LetterScript.js"></script> in the <head> part of your ..aspx file. That's it.

    Eliyahu

    "Peter" <> wrote in message news:...
    I have the following code that executes JavaScript, is there a way I can put the same JavaScript code in it's own file and execute the code in that file instead of having this JavaScript code in my C# code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.Page.IsClientScriptBlockRegistered("LetterScript"))
    {
    this.Page.RegisterClientScriptBlock(
    "LetterScript",
    "<script language=javascript>" +
    "function CreateLetter(){" +
    "var word = new ActiveXObject(\"Word.Application\");" +
    "word.Visible = true; " +
    "var file = document.getElementById('txtTemplate').value; " +
    "var objDoc = word.Documents.Add(file); " +
    "objDoc.FormFields(\"FirstName\").Result = document.getElementById('txtFirstName').value; " +
    "objDoc.FormFields(\"LastName\").Result = document.getElementById('txtLastName').value; " +
    "} " +
    "self.focus(); " +
    "</script>");
    }

    this.Button1.Attributes.Add("onClick", "CreateLetter()");

    }


    Thanks


    Peter



    Thank you very much, this is what I was looking for!
    Peter, Jan 17, 2006
    #6
    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. mcdeveloper
    Replies:
    1
    Views:
    4,189
    mcdeveloper
    Jun 13, 2006
  2. CRON
    Replies:
    24
    Views:
    200,724
    Adrienne Boswell
    Jun 20, 2006
  3. Mark Rae

    JavaScript or not JavaScript

    Mark Rae, Sep 5, 2006, in forum: ASP .Net
    Replies:
    36
    Views:
    1,090
    Paul Sture
    Sep 9, 2006
  4. Nathan Sokalski
    Replies:
    4
    Views:
    579
    PJ on Development
    Nov 8, 2007
  5. Lisa Calla
    Replies:
    0
    Views:
    272
    Lisa Calla
    Nov 9, 2004
Loading...

Share This Page