473,769 Members | 5,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recieving Error "'theForm' is undefined" In ASP.NET Application Even With The Line "var theForm = document.forms['form1'];"

I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I do a View Source one of the <scriptelemen ts is as follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(ev entTarget, eventArgument) {
if (!theForm.onsub mit || (theForm.onsubm it() != false)) {
theForm.__EVENT TARGET.value = eventTarget;
theForm.__EVENT ARGUMENT.value = eventArgument;
theForm.submit( );
}
}
// -->
</script>

You will notice that the first line of code in this script element creates
and assigns a value to a variable named "theForm". This code was generated
by ASP.NET, so it SHOULD be correct, and it looks to me like 'theForm' is
defined. I have seen other people post this error before also, but I can't
remember what they said they did to fix it. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
Jun 10 '07 #1
5 10806
Nathan Sokalski said the following on 6/9/2007 8:57 PM:
I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I do a View Source one of the <scriptelemen ts is as follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(ev entTarget, eventArgument) {
if (!theForm.onsub mit || (theForm.onsubm it() != false)) {
theForm.__EVENT TARGET.value = eventTarget;
theForm.__EVENT ARGUMENT.value = eventArgument;
theForm.submit( );
}
}
// -->
</script>

You will notice that the first line of code in this script element creates
and assigns a value to a variable named "theForm". This code was generated
by ASP.NET, so it SHOULD be correct,
But it isn't "correct". The error is caused because the script block
will be before the form element appears in the page and so it will be
undefined because it doesn't exist yet.
and it looks to me like 'theForm' is defined.
If, and only if, a form with the name/id attribute of "form1" exists at
the time that the script executes.
I have seen other people post this error before also, but I can't
remember what they said they did to fix it. Any ideas? Thanks.
1) Make sure the script block appears after the element in the page.
2) Better, is to include it in the postback function. Failing that, wrap
it in a function and call it via window.onload

The entire if part that is not in a function is a bad way of writing a
script though. I would be interested to see a scenario where theForm
didn't get defined on the first line but got defined on the second.
Meaning, a browser that didn't support document.forms['form1'] but did
support document.form1

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 10 '07 #2
Thank you for that information, it will definitely be useful at some point.
However, it brings me to another problem:

Because I did not specifically request that this script be added (it gets
added automatically, my code does not include anything that says where to
put the script or even that it should be included at all), what should I do
to fix the problem? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Randy Webb" <Hi************ @aol.comwrote in message
news:yb******** ************@te lcove.net...
Nathan Sokalski said the following on 6/9/2007 8:57 PM:
>I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I do a View Source one of the <scriptelemen ts is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(ev entTarget, eventArgument) {
if (!theForm.onsub mit || (theForm.onsubm it() != false)) {
theForm.__EVENT TARGET.value = eventTarget;
theForm.__EVENT ARGUMENT.value = eventArgument;
theForm.submit( );
}
}
// -->
</script>

You will notice that the first line of code in this script element
creates and assigns a value to a variable named "theForm". This code was
generated by ASP.NET, so it SHOULD be correct,

But it isn't "correct". The error is caused because the script block will
be before the form element appears in the page and so it will be undefined
because it doesn't exist yet.
>and it looks to me like 'theForm' is defined.

If, and only if, a form with the name/id attribute of "form1" exists at
the time that the script executes.
>I have seen other people post this error before also, but I can't
remember what they said they did to fix it. Any ideas? Thanks.

1) Make sure the script block appears after the element in the page.
2) Better, is to include it in the postback function. Failing that, wrap
it in a function and call it via window.onload

The entire if part that is not in a function is a bad way of writing a
script though. I would be interested to see a scenario where theForm
didn't get defined on the first line but got defined on the second.
Meaning, a browser that didn't support document.forms['form1'] but did
support document.form1

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Jun 10 '07 #3
Nathan Sokalski said the following on 6/9/2007 9:55 PM:
Thank you for that information, it will definitely be useful at some point.
However, it brings me to another problem:

Because I did not specifically request that this script be added (it gets
added automatically, my code does not include anything that says where to
put the script or even that it should be included at all), what should I do
to fix the problem? Thanks.
Stop using ASP.NET :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 10 '07 #4
Make sure you have something like
<form id="form1" runat="server">
on your form. You didn't rename the form1 did you?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
>I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I do a View Source one of the <scriptelemen ts is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(ev entTarget, eventArgument) {
if (!theForm.onsub mit || (theForm.onsubm it() != false)) {
theForm.__EVENT TARGET.value = eventTarget;
theForm.__EVENT ARGUMENT.value = eventArgument;
theForm.submit( );
}
}
// -->
</script>

You will notice that the first line of code in this script element creates
and assigns a value to a variable named "theForm". This code was generated
by ASP.NET, so it SHOULD be correct, and it looks to me like 'theForm' is
defined. I have seen other people post this error before also, but I can't
remember what they said they did to fix it. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

Jun 10 '07 #5
No, I did not rename my the form. The exact line of code copy & pasted from
Visual Studio 2005 is:

<form id="form1" runat="server">

--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Eliyahu Goldin" <RE************ **************@ mMvVpPsS.orgwro te in
message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Make sure you have something like
<form id="form1" runat="server">
on your form. You didn't rename the form1 did you?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:eQ******** ******@TK2MSFTN GP05.phx.gbl...
>>I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I do a View Source one of the <scriptelemen ts is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(ev entTarget, eventArgument) {
if (!theForm.onsub mit || (theForm.onsubm it() != false)) {
theForm.__EVENT TARGET.value = eventTarget;
theForm.__EVENT ARGUMENT.value = eventArgument;
theForm.submit( );
}
}
// -->
</script>

You will notice that the first line of code in this script element
creates and assigns a value to a variable named "theForm". This code was
generated by ASP.NET, so it SHOULD be correct, and it looks to me like
'theForm' is defined. I have seen other people post this error before
also, but I can't remember what they said they did to fix it. Any ideas?
Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/


Jun 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4035
by: Jon Maz | last post by:
Hi, I have written a page to test a function of mine that strips non-English accents and various other characters out of a given string. The function itself (called 'StripAll' in the code below) now works fine. However something else in the test page is not working, and it's starting to bug me! If the user types a string containing double quotes into the textbox, when the textbox is re-populated after the postback, any part of the...
5
2606
by: comshiva | last post by:
Hi all, I have converted my existing ASP.NET project from 1.1 to 2.0 and i have found that everything works fine except the linkbutton control in my datagrid which throws an javascript error when clicked. I thought the control might be the problem, so i deleted the old control and binded the new linkbutton control but am still getting the same error. Am using visual studio 2005. Source code inside my grid:...
7
2145
by: comzy | last post by:
I have created an event bubbling for my pager control which is used for implementing paging in data grid. Althoug it worked very fine in .NET 1.1 it is throwing the following error after i migrated it to .NET 2.0 The peculiar thing abou that is it is throwing error only in IE 6.0 and it works fine in netscape, firefox, opera even after converting it to ..NET 2.0 Can anyone tell me whether anything has changed in event bubbling format...
2
7718
by: bay_dar | last post by:
Hi, I have an internal ASP.NET application that I'm are using to send e-mails out based on a single milepost or milepost range entered. I'm trying to do two things when a user clicks on the submit button to submit a form that contains one or two Mileposts: 1) If a Milepost range larger than 5 miles is entered, I would like to pop up a confirmation box to confirm the range.
2
1987
by: manojsingh | last post by:
hi, I am developing on a Payroll system and using Jsp/servlet technology. For this I have created a Employee Information Page which has three buttons Add Employee Information , Modifiy Employee Information and Delete Employee Information. There is a JavaScript function which executes on BODY tag's onload function. // -- <body onload="funAdd()" --// function funAdd() {
11
3164
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global" storage is ButtonHasBeenClicked. In this simple example code in Form1 calls a routine in Module1 which then calls code back in Form1 (subroutine WhatEver). WhatEver needs to access ButtonHasBeenClicked but the reference to ButtonHasBeenClicked...
57
4769
by: HEX | last post by:
Have a site under development which works with both IE and Mozilla Firefox. Three MAC users accessed site and two have a small problem with one page and the other recently went to the new Leopard release with Safari V3.0.4. browser. This user has big problems with a couple of pages. A couple of users have older Safari and one has Firefox. They experience the minor issue. Is developing a site with Windows and targeting IE and Firefox as the...
18
7091
by: hsegoy1979 | last post by:
Dear All Iam new to asp.net. I had a problem. I have a multiline textbox and link buttons say firstname,lastname, Emails and corresponding hidden fields with each textbox.My problem is that suppose i typed some text afterwards i press linkbutton firstname(say).so the value in hidden field associated with this link button will display in the text box. Thankx In Advance Regards Yogesh
4
8420
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even onactivetabindexchanged we have called a method loadtabpanel() which is defined in javascript in same page.the problem is it sometime give the message load tab panel undefined. this error does not come regularly. it comes in usercontrol rendering . i...
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.