How to prevent the enter key causing a form postback .net 1.1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SnVsaWEgQg==?=

    How to prevent the enter key causing a form postback .net 1.1

    Hi all

    I've got a very complex form with loads of text boxes and buttons on it.
    It's causing me all sorts of problems when users press the enter key and
    cause a postback.

    I need to disable the enter key. I've done some surfing and found some
    Microsoft recommended code but it doesn't seem to work and I'm wondering what
    I've done wrong. If possible I'd like to disable the enter key for the whole
    form not for each text box (that would be sooooo complicated to implement).

    I'm using .Net 1.1 and vb.

    Here's what I've got at the moment.

    <HEAD>
    <title>Raise CSD</title>
    <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
    <meta content="JavaSc ript" name="vs_defaul tClientScript">
    <meta content="http://schemas.microso ft.com/intellisense/ie5"
    name="vs_target Schema">
    <SCRIPT language="javas cript">
    function DisableKeyPress ()
    {
    //alert(window.ev ent.keyCode)
    if (event.keyCode == 13)
    {
    event.cancelbub ble = true;
    event.returnval ue = false;
    }
    }
    </SCRIPT>
    </HEAD>
    <body onKeyPress="Dis ableKeyPress()" vLink="#000000" aLink="#d9d5d2"
    link="#d7451a" MS_POSITIONING= "GridLayout ">
    <form id="Form1" method="post" runat="server" >

    then the code for the rest of the form.....

    Thanks in advance!
    Julia
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: How to prevent the enter key causing a form postback .net 1.1

    In the past, I did this with JavaScript, similar to what you have here.
    Only, as I remember it, it wired up the Cancel event on body load and not
    for each key press. I would google JavaScript enter key or even
    event.keyCode == 13 for more suggestions.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
    news:6F484FC9-4FE0-486E-B311-B3EAC624433E@mi crosoft.com...
    Hi all
    >
    I've got a very complex form with loads of text boxes and buttons on it.
    It's causing me all sorts of problems when users press the enter key and
    cause a postback.
    >
    I need to disable the enter key. I've done some surfing and found some
    Microsoft recommended code but it doesn't seem to work and I'm wondering
    what
    I've done wrong. If possible I'd like to disable the enter key for the
    whole
    form not for each text box (that would be sooooo complicated to
    implement).
    >
    I'm using .Net 1.1 and vb.
    >
    Here's what I've got at the moment.
    >
    <HEAD>
    <title>Raise CSD</title>
    <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
    <meta content="JavaSc ript" name="vs_defaul tClientScript">
    <meta content="http://schemas.microso ft.com/intellisense/ie5"
    name="vs_target Schema">
    <SCRIPT language="javas cript">
    function DisableKeyPress ()
    {
    //alert(window.ev ent.keyCode)
    if (event.keyCode == 13)
    {
    event.cancelbub ble = true;
    event.returnval ue = false;
    }
    }
    </SCRIPT>
    </HEAD>
    <body onKeyPress="Dis ableKeyPress()" vLink="#000000" aLink="#d9d5d2"
    link="#d7451a" MS_POSITIONING= "GridLayout ">
    <form id="Form1" method="post" runat="server" >
    >
    then the code for the rest of the form.....
    >
    Thanks in advance!
    Julia

    Comment

    • =?Utf-8?B?SnVsaWEgQg==?=

      #3
      Re: How to prevent the enter key causing a form postback .net 1.1


      Have found this and it works :-)

      <Script language=javasc ript>
      //this disables the enter key to stop form submission when the user
      presses enter
      document.onkeyd own = doKey;

      function doKey(E) {
      if (event.keyCode= =13)return false;
      }
      </script>

      Thanks!
      "Cowboy (Gregory A. Beamer)" wrote:
      In the past, I did this with JavaScript, similar to what you have here.
      Only, as I remember it, it wired up the Cancel event on body load and not
      for each key press. I would google JavaScript enter key or even
      event.keyCode == 13 for more suggestions.
      >
      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA
      >
      Subscribe to my blog

      >
      or just read it:

      >
      *************** *************** **************
      | Think outside the box! |
      *************** *************** **************
      "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
      news:6F484FC9-4FE0-486E-B311-B3EAC624433E@mi crosoft.com...
      Hi all

      I've got a very complex form with loads of text boxes and buttons on it.
      It's causing me all sorts of problems when users press the enter key and
      cause a postback.

      I need to disable the enter key. I've done some surfing and found some
      Microsoft recommended code but it doesn't seem to work and I'm wondering
      what
      I've done wrong. If possible I'd like to disable the enter key for the
      whole
      form not for each text box (that would be sooooo complicated to
      implement).

      I'm using .Net 1.1 and vb.

      Here's what I've got at the moment.

      <HEAD>
      <title>Raise CSD</title>
      <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
      <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
      <meta content="JavaSc ript" name="vs_defaul tClientScript">
      <meta content="http://schemas.microso ft.com/intellisense/ie5"
      name="vs_target Schema">
      <SCRIPT language="javas cript">
      function DisableKeyPress ()
      {
      //alert(window.ev ent.keyCode)
      if (event.keyCode == 13)
      {
      event.cancelbub ble = true;
      event.returnval ue = false;
      }
      }
      </SCRIPT>
      </HEAD>
      <body onKeyPress="Dis ableKeyPress()" vLink="#000000" aLink="#d9d5d2"
      link="#d7451a" MS_POSITIONING= "GridLayout ">
      <form id="Form1" method="post" runat="server" >

      then the code for the rest of the form.....

      Thanks in advance!
      Julia
      >
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: How to prevent the enter key causing a form postback .net 1.1

        "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
        news:555503CF-26FE-4FC4-B654-642ABFC33091@mi crosoft.com...

        [top-posting corrected]
        >>I need to disable the Enter key.
        >>
        >In the past, I did this with JavaScript, similar to what you have here.
        >Only, as I remember it, it wired up the Cancel event on body load and not
        >for each key press. I would google JavaScript enter key or even
        >event.keyCod e == 13 for more suggestions.
        >
        Have found this and it works :-)
        >
        <Script language=javasc ript>
        The above script declaration has been deprecated for ages - use this
        instead:

        <script type="text/javascript">
        if (event.keyCode= =13)return false;
        This is not cross-browser compatible - it will work only in IE:



        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Cowboy \(Gregory A. Beamer\)

          #5
          Re: How to prevent the enter key causing a form postback .net 1.1

          Just be sure to test on other browsers, as some JavaScript only works with
          some. Worst case is creating branches for different browsers.

          --
          Gregory A. Beamer
          MVP, MCP: +I, SE, SD, DBA

          Subscribe to my blog


          or just read it:


          *************** *************** **************
          | Think outside the box! |
          *************** *************** **************
          "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
          news:555503CF-26FE-4FC4-B654-642ABFC33091@mi crosoft.com...
          >
          Have found this and it works :-)
          >
          <Script language=javasc ript>
          //this disables the enter key to stop form submission when the user
          presses enter
          document.onkeyd own = doKey;
          >
          function doKey(E) {
          if (event.keyCode= =13)return false;
          }
          </script>
          >
          Thanks!
          "Cowboy (Gregory A. Beamer)" wrote:
          >
          >In the past, I did this with JavaScript, similar to what you have here.
          >Only, as I remember it, it wired up the Cancel event on body load and not
          >for each key press. I would google JavaScript enter key or even
          >event.keyCod e == 13 for more suggestions.
          >>
          >--
          >Gregory A. Beamer
          >MVP, MCP: +I, SE, SD, DBA
          >>
          >Subscribe to my blog
          >http://feeds.feedburner.com/GregoryBeamer#
          >>
          >or just read it:
          >http://feeds.feedburner.com/GregoryBeamer
          >>
          >************** *************** ***************
          >| Think outside the box! |
          >************** *************** ***************
          >"Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
          >news:6F484FC 9-4FE0-486E-B311-B3EAC624433E@mi crosoft.com...
          Hi all
          >
          I've got a very complex form with loads of text boxes and buttons on
          it.
          It's causing me all sorts of problems when users press the enter key
          and
          cause a postback.
          >
          I need to disable the enter key. I've done some surfing and found some
          Microsoft recommended code but it doesn't seem to work and I'm
          wondering
          what
          I've done wrong. If possible I'd like to disable the enter key for the
          whole
          form not for each text box (that would be sooooo complicated to
          implement).
          >
          I'm using .Net 1.1 and vb.
          >
          Here's what I've got at the moment.
          >
          <HEAD>
          <title>Raise CSD</title>
          <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
          <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
          <meta content="JavaSc ript" name="vs_defaul tClientScript">
          <meta content="http://schemas.microso ft.com/intellisense/ie5"
          name="vs_target Schema">
          <SCRIPT language="javas cript">
          function DisableKeyPress ()
          {
          //alert(window.ev ent.keyCode)
          if (event.keyCode == 13)
          {
          event.cancelbub ble = true;
          event.returnval ue = false;
          }
          }
          </SCRIPT>
          </HEAD>
          <body onKeyPress="Dis ableKeyPress()" vLink="#000000" aLink="#d9d5d2"
          link="#d7451a" MS_POSITIONING= "GridLayout ">
          <form id="Form1" method="post" runat="server" >
          >
          then the code for the rest of the form.....
          >
          Thanks in advance!
          Julia
          >>
          >>

          Comment

          • =?Utf-8?B?SnVsaWEgQg==?=

            #6
            Re: How to prevent the enter key causing a form postback .net 1.1

            This should be OK for me as it's for an intranet application and all users
            will using IE.

            Thanks for the warning anyway.

            Julia

            "Cowboy (Gregory A. Beamer)" wrote:
            Just be sure to test on other browsers, as some JavaScript only works with
            some. Worst case is creating branches for different browsers.
            >
            --
            Gregory A. Beamer
            MVP, MCP: +I, SE, SD, DBA
            >
            Subscribe to my blog

            >
            or just read it:

            >
            *************** *************** **************
            | Think outside the box! |
            *************** *************** **************
            "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
            news:555503CF-26FE-4FC4-B654-642ABFC33091@mi crosoft.com...

            Have found this and it works :-)

            <Script language=javasc ript>
            //this disables the enter key to stop form submission when the user
            presses enter
            document.onkeyd own = doKey;

            function doKey(E) {
            if (event.keyCode= =13)return false;
            }
            </script>

            Thanks!
            "Cowboy (Gregory A. Beamer)" wrote:
            In the past, I did this with JavaScript, similar to what you have here.
            Only, as I remember it, it wired up the Cancel event on body load and not
            for each key press. I would google JavaScript enter key or even
            event.keyCode == 13 for more suggestions.
            >
            --
            Gregory A. Beamer
            MVP, MCP: +I, SE, SD, DBA
            >
            Subscribe to my blog

            >
            or just read it:

            >
            *************** *************** **************
            | Think outside the box! |
            *************** *************** **************
            "Julia B" <JuliaB@discuss ions.microsoft. comwrote in message
            news:6F484FC9-4FE0-486E-B311-B3EAC624433E@mi crosoft.com...
            Hi all

            I've got a very complex form with loads of text boxes and buttons on
            it.
            It's causing me all sorts of problems when users press the enter key
            and
            cause a postback.

            I need to disable the enter key. I've done some surfing and found some
            Microsoft recommended code but it doesn't seem to work and I'm
            wondering
            what
            I've done wrong. If possible I'd like to disable the enter key for the
            whole
            form not for each text box (that would be sooooo complicated to
            implement).

            I'm using .Net 1.1 and vb.

            Here's what I've got at the moment.

            <HEAD>
            <title>Raise CSD</title>
            <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
            <meta content="JavaSc ript" name="vs_defaul tClientScript">
            <meta content="http://schemas.microso ft.com/intellisense/ie5"
            name="vs_target Schema">
            <SCRIPT language="javas cript">
            function DisableKeyPress ()
            {
            //alert(window.ev ent.keyCode)
            if (event.keyCode == 13)
            {
            event.cancelbub ble = true;
            event.returnval ue = false;
            }
            }
            </SCRIPT>
            </HEAD>
            <body onKeyPress="Dis ableKeyPress()" vLink="#000000" aLink="#d9d5d2"
            link="#d7451a" MS_POSITIONING= "GridLayout ">
            <form id="Form1" method="post" runat="server" >

            then the code for the rest of the form.....

            Thanks in advance!
            Julia
            >
            >
            >
            >

            Comment

            Working...