Solved

Inserting JavaScript in a C# ASP.NET file

Posted on 2008-06-17
1
229 Views
Last Modified: 2013-12-16
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
Comment
Question by:angelsofgrace2006
1 Comment
 
LVL 7

Accepted Solution

by:
alexpercsi earned 500 total points
ID: 21818416
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question