• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Inserting JavaScript in a C# ASP.NET file

Hi,

I need to add a little JavaScript function in a C# asp.net file (part of a website).

This file doesn't allow for the <head>, <body> or <html> elements. It gives me the error:
"Only content controls are allowed directly in a content page that contains Content controls".

Meanwhile, a nice chap here told me to put in the <head> scetion of the master page the following:
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>

and in the <body>:
<asp:ContentPlaceHolder id="body" runat="server">
</asp:ContentPlaceHolder>

So I did.

And in the .aspx file i put the following just for the sake of testing:

<asp:Content ID="head" ContentPlaceHolderID="head" runat="server">

<script type='text/javascript'>

 function check()

{

   var txtBox1 = document.getElementById('<%= Email.ClientID %>');
   alert(txtBox1.value);
}  

</script>

</asp:Content>

 

And in the markup of the Email field of the registration form, I've added onclick:

<asp:TextBox id="Email" runat="server" MaxLength="128" columns="40" onclick='check' />

The problem is that I always het the error:
"The name Email [the id name of the first email textbox] doesn't exist in the current context".

What am I missing here ? Where to put the JavaScript code ?

Anyway, if you want to see what I'm trying to do with the .aspx file (the new user registration form), please check http://www.absinto.org/form.html (start filling the login box).

Any help would be appreciated. I really need this very much.

Warm Regards,
Mário Gamito
0
angelsofgrace2006
Asked:
angelsofgrace2006
1 Solution
 
alexpercsiCommented:
Your javascript seems to be placed just fine.

However, controls with the runat="server" attribute usually get their IDs prefix with the parent controls' ids. So your textbox is probably no longer called Email anymore. The good news is that the ID will not change as long as you do not change any ID of any parent control. This said, you can find out the final ID of your textbox by clicking View Source in your browser while looking at the page. Just look through your HTML and find the textbox and see what ID it was given.

I hope this helps.

Best Regards,
Alex.

P.S. I just noticed that your example seems to be working just fine under Firefox 3. Have you changed it since you have posted your question?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now