473,416 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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 <scriptelements is as follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.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********@hotmail.com
http://www.nathansokalski.com/
Jun 10 '07 #1
5 10780
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 <scriptelements is as follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.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.javascript 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********@hotmail.com
http://www.nathansokalski.com/

"Randy Webb" <Hi************@aol.comwrote in message
news:yb********************@telcove.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 <scriptelements is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.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.javascript 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.javascript 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********@hotmail.comwrote in message
news:eQ**************@TK2MSFTNGP05.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 <scriptelements is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.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********@hotmail.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********@hotmail.com
http://www.nathansokalski.com/

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:%2****************@TK2MSFTNGP03.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********@hotmail.comwrote in message
news:eQ**************@TK2MSFTNGP05.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 <scriptelements is as
follows:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.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********@hotmail.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
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)...
5
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...
7
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...
2
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...
2
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...
11
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"...
57
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...
18
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...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
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...

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.