Solved

Inserting JavaScript in a C# ASP.NET file

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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