Recieving Error: 'theForm' is undefined

Discussion in 'ASP .Net' started by Nathan Sokalski, Jun 11, 2007.

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

    'theForm' is undefined

    However, when I did a View Source I noticed that ASP.NET added the following
    code:

    <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>

    I was told in a previous posting that the reason for the error is that the
    script containing the declaration and assigning of a value to it occur
    before the

    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Jun 11, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    Scott M. Guest

    If the page does not have a form named "form1", then the variable
    declaration will fail and the variable "theForm" won't be able to be
    created.


    var theForm = document.forms['form1'];


    "Nathan Sokalski" <> wrote in message
    news:...
    >I have an ASP.NET application which is giving the following JavaScript
    >error:
    >
    > 'theForm' is undefined
    >
    > However, when I did a View Source I noticed that ASP.NET added the
    > following code:
    >
    > <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>
    >
    > I was told in a previous posting that the reason for the error is that the
    > script containing the declaration and assigning of a value to it occur
    > before the
    >
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Scott M., Jun 11, 2007
    #2
    1. Advertising

  3. Nathan Sokalski

    bruce barker Guest

    either the script referencing theForm occurs before the declaration or
    form1 is not defined (a form tag with name form1).


    -- bruce (sqlwork.com)


    Nathan Sokalski wrote:
    > I have an ASP.NET application which is giving the following JavaScript
    > error:
    >
    > 'theForm' is undefined
    >
    > However, when I did a View Source I noticed that ASP.NET added the following
    > code:
    >
    > <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>
    >
    > I was told in a previous posting that the reason for the error is that the
    > script containing the declaration and assigning of a value to it occur
    > before the
    >
     
    bruce barker, Jun 11, 2007
    #3
  4. Nathan Sokalski

    Scott M. Guest

    And, remember "Form1" != "form1".



    "Scott M." <> wrote in message
    news:...
    > If the page does not have a form named "form1", then the variable
    > declaration will fail and the variable "theForm" won't be able to be
    > created.
    >
    >
    > var theForm = document.forms['form1'];
    >
    >
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >>I have an ASP.NET application which is giving the following JavaScript
    >>error:
    >>
    >> 'theForm' is undefined
    >>
    >> However, when I did a View Source I noticed that ASP.NET added the
    >> following code:
    >>
    >> <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>
    >>
    >> I was told in a previous posting that the reason for the error is that
    >> the script containing the declaration and assigning of a value to it
    >> occur before the
    >>
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
     
    Scott M., Jun 11, 2007
    #4
  5. On Jun 11, 2:24 am, "Nathan Sokalski" <> wrote:
    > I have an ASP.NET application which is giving the following JavaScript
    > error:
    >
    > 'theForm' is undefined
    >
    > However, when I did a View Source I noticed that ASP.NET added the following
    > code:
    >
    > <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>
    >
    > I was told in a previous posting that the reason for the error is that the
    > script containing the declaration and assigning of a value to it occur
    > before the
    >
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/


    try

    var theForm = document.getElementById("form1");
     
    Alexey Smirnov, Jun 11, 2007
    #5
  6. He can't try that, Alexei, because the code is being injected by ASP.NET.

    He can't change the embedded javascript funtions, if he's using ASP.NET 2.0.
    There's a chance he mioght be able to do that, if he's using ASP.NET 1.1.

    All he'd have to do is edit the scripts located in
    \wwwroot\aspnet_client\system_web\1_1_4322.

    The problem doesn't seem to be that the function is buggy, though.

    If Nathan were to rename the page's form to "form2",
    instead of naming it "form1", he should see this :

    <script type="text/javascript">
    <!--
    var theForm = document.forms['form2'];
    if (!theForm) {
    theForm = document.form2;
    }
    function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
    }
    }
    // -->
    </script>

    Notice that the source reveals that the script has picked up the form's new name.

    If that doesn't show up, and the error persists, it's a timing problem.
    The javascript is running before the form has been rendered.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ======================================
    "Alexey Smirnov" <> wrote in message
    news:...
    > On Jun 11, 2:24 am, "Nathan Sokalski" <> wrote:
    >> I have an ASP.NET application which is giving the following JavaScript
    >> error:
    >>
    >> 'theForm' is undefined
    >>
    >> However, when I did a View Source I noticed that ASP.NET added the following
    >> code:
    >>
    >> <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>
    >>
    >> I was told in a previous posting that the reason for the error is that the
    >> script containing the declaration and assigning of a value to it occur
    >> before the
    >>
    >> --
    >> Nathan Sokalski
    >> ://www.nathansokalski.com/

    >
    > try
    >
    > var theForm = document.getElementById("form1");
    >
     
    Juan T. Llibre, Jun 11, 2007
    #6
  7. On Jun 11, 3:01 pm, "Juan T. Llibre" <>
    wrote:
    > He can't try that, Alexei, because the code is being injected by ASP.NET.
    >


    On Jun 11, 2:24 am, "Nathan Sokalski" <> wrote:
    > However, when I did a View Source I noticed that ASP.NET added the following
    > code:


    Okay, I see. Sorry!
     
    Alexey Smirnov, Jun 11, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitch
    Replies:
    13
    Views:
    1,231
    Mitch
    Mar 10, 2006
  2. Ben
    Replies:
    2
    Views:
    724
    Karl Zimmerman
    Sep 27, 2004
  3. Andrew
    Replies:
    1
    Views:
    258
    Peter Hansen
    Dec 19, 2005
  4. divya
    Replies:
    1
    Views:
    353
    Cowboy \(Gregory A. Beamer\)
    Sep 19, 2006
  5. Nathan Sokalski
    Replies:
    5
    Views:
    724
    Nathan Sokalski
    Jun 10, 2007
Loading...

Share This Page