JavaScript

P

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
 
L

Lau Lei Cheong

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" <[email protected]>
???????:[email protected]...
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
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| 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: <[email protected]>
| 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
|
 
E

Eliyahu Goldin

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

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
 
K

Kevin Spencer

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.

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
 
P

Peter

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

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

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top