Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to include a .js file into an aspx page? 1

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
Somebody can help me in how can I include a .js file in my aspx page so that to trigger a javascript validation? And also the best event that I able to use, for instance: OnClick, OnSubmit,..

I have read an article from msdn that says if you want to include a .js file in an ASP.NET page you have to do it thus:
VB.NET
Code:
Page.RegisterClientScriptBlock("MyScript", _
   &quot;<script language=javascript src='MyJavaScriptFile.js'>&quot;)

But this doesn' t run.
Now I use:
Code:
<form id=&quot;form1&quot; OnSubmit=&quot; return jsFunctionName()&quot; runat=&quot;server&quot;> </form>

Thank you
 
If you put this code
Code:
<script language=javascript src='MyJavaScriptFile.js'>
in the head of your html, does the function get called?

I believe that RegisterClientScriptBlock simply puts the string somewhere in the html. How you use that script is still up to you - i.e. you still need to wire-up the events to the functions.

For instance, if you had a Button control called Button1 that needs to call a function from your javascript file, you should do this in code-behind:
Code:
Button1.Attributes.Add(&quot;onclick&quot;, &quot;javascript: functionName();&quot;)
 
What you have to do is:

1 - Right-click on your project in the project explorer and select &quot;Add New Item&quot;.

2 - From the avaiable templates select &quot;JScript file&quot;. This will add a *.js file to your project, where you could write your javascript functions. Then, assuming that you name your file &quot;MyScripts.js&quot;, the third step is...

3 - View the HTML source code of the page where you'll be accessing the scripts file and add the follwing to the <HEAD> tag :
Code:
<HEAD>
...
<script language=&quot;javascript&quot; id=&quot;MyScripts&quot; src=&quot;MyScripts.js&quot;>
</script>
</HEAD>
Once you do this, the functions you write in your file will be avaible in your page. Thus if you have a function in the file called MyFunction(), you could do the following in the code behind file:
Code:
   btn.Attributes.Add(&quot;onclick&quot;, &quot;javascript:MyFunction(); return false;&quot;);

Assuming that btn is a Button control, clicking on it will run the code in MyFunction() which is found in your MyScripts.js file. Adding &quot;return false&quot; next to the function cancels the post back so that only client side code runs. If you wish to allow the post back when clicking the button, remove the &quot;return false&quot; from the string above.

Hope this helps!

JC

We don't see things as they are; we see them as we are. - Anais Nin
 
Ok thanks, the validation runs with your code but in the alert messages characters like (ñ, Ñ, é, è, ü, etc,..) from the Spanish language that I use are omitted. I don' t understand why this characters are omitted. For instance in words like 'cañón' appears 'can' from the alert message text.
I need these characters :), thanks.
 
Is it the same if I call the function thus?:
Code:
<form id=&quot;form1&quot; OnSubmit=&quot;return MyFunction()&quot; runat=&quot;server&quot;>
</form>

And there is something wrong if I put one 'codepage' at the top of the page?
Code:
<%@ Page Language=&quot;VB&quot; CODEPAGE=&quot;1252&quot;%>

If I put this 'codepage' the alert messages show all the characters fine, but then the data filled in the form and stored in a dataset or after in the database it does the same, these characters (ñ, Ñ, é, ü,etc..) are omitted.

I don't know what can I do :-(, please help.

Thanks,
Cesar
 
Cesar,
I don't know exactly how to solve your problem right now, but I'll look into it and let you know.

JC

We don't see things as they are; we see them as we are. - Anais Nin
 
Thank you very much JCruz :). While I continue searching too. I am a little bit desperate after searching and asking during many hours along one month.

I will wait for your answer, no problem if you do not find anything.

Thanks you,
Cesar
mail: cesar_casafont@hotmail.com
 
check for this line in your page

<META HTTP-EQUIV=&quot;Content-Type&quot;
CONTENT=&quot;text/html; CHARSET=windows-1251&quot;>

 
Hi,

I have written your code between the <head> tags and it does not run.
But I have Dreamweaver MX and I have created a new ASP.NET VB document with it, and automatically Dreamweaver has written into the page:

Code:
<%@ Page Language=&quot;VB&quot; ContentType=&quot;text/html&quot; ResponseEncoding=&quot;iso-8859-1&quot; %>
<html>
<head>
<title></title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
...

I have copied it and pasted into my page and the alerts ran fine, with all characters, but another time the data stored inside the dataset and inside the database it lacks these characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top