Solved

Inserting JavaScript in a C# ASP.NET file

Posted on 2008-06-17
1
225 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now