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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET and JavaScript

Status
Not open for further replies.

Modica82

Technical User
Jan 31, 2003
410
GB
Hi all,

i need some help on understanding the way ASP.NET deals with JavaScript. Now do i have to create the JavaScript within my aspx pages, as i would normally when creating asp or HTML pages or do i have to construct functions within a code behind file.

Can anyone shed any light on the matter.

Kind Regards,

Roberto
 
You can add javascript functions in the same way you would in a normal .htm or .asp page by including a script block in the aspx file like below
Code:
<script type=&quot;text/javascript&quot;>
//some javascript code
</script>
However remember for the script to be run client side you must NOT use the runat attribute which has only one value, 'server'.

You can also link to a .js file as normal using
Code:
<script type=&quot;text/javascript&quot; src=&quot;myjavascriptfile.js&quot;></script>

In addition the class library provides some functionality to add client script blocks to the page programtically from your code behind. Check out Page.RegisterClientScriptBlock. Here is a link to msdn covering the topic.


hope this helps

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Additionally, I have found it useful for certain needs to embed a javascript call in the codebehind. There are limits and probably isn't recommended by most (and probably isn't supported by all browsers), but you could do something like this.

Let's say that you have a popup window that you want to close (or move or resize) after the postback. You can send the javascript code through a Response.Write to do those things. Here's an example:

Response.Write(&quot;<script>if (window.opener) {window.opener.document.location.reload(); window.resizeTo(500,500); window.moveTo(window.screenLeft-100,window.screenTop-100);}</script>&quot;)

This code resizes the window, moves it up and over a little (from the original window position) and reloads the parent.

Another example below refreshes the parent and closes the popup window:
Response.Write(&quot;<script>if (window.opener) {window.opener.document.location.reload(); window.close();}</script>&quot;)

Just some examples of using javascript in the codebehind.

If anyone has any constructive criticism, please feel free. If it is to blast my example, don't bother responding.

Mark
 
Hi Mark

Not to blast your example at all :) just to offer an additional idea.

You can add javascript to buttons, dropdowns etc. in your code-behind by the following process:

[control].Attributes.Add(&quot;eventname&quot;, &quot;action&quot;);

Where
control would be the name of your control (e.g. btnSave)
eventname is the javascript event you want to add e.g. onclick
action is the javascript code to execute e.g. if you have a javascript function called validateThis() in your aspx page - you can add it like:
btnSave.Attributes.Add(&quot;onclick&quot;, &quot;javascript: return validateThis();&quot;);

Hope that helps!

Craftor

:cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top