Posts Tagged ‘key’

Submit Form On Enter Key Solution

It is natural for web users to hit the enter key to submit a form or otherwise activate an action. There have been several cases where the form does not submit on the enter key. The below example works.

<body>
   <form id="form1" runat="server">
      <div>
         <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
         <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
         <asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
      </div>
   </form>
</body>

The Button1_Click action simply copies the input text from txtFirstName into the label lblOutput. Because this simple example works, one would expect this to work in every case. That is not the case. If hitting the enter key does not submit the form, try one of the following below:

  1. Try putting an invisible HTML Input (Submit) button. Though this has worked for others, it did not work for my situation.
    <input type="text" style="display:none"/>
  2. A second option to try is to use a onKeyDown event. This second option will append a moderately long javascript script to check for the enter key on each button press. If the button is pressed, it activates the form submit. To use this, place this script in the head. Finally, on the last input textbox, use the following:
    onKeyPress="return submitFormWithEnter(this,event)"

    The script is below goes in the within html head tags:

    <script type="text/javascript">
    
    function submitFormWithEnter(myfield,e)
    {
       var keycode;
       if (window.event)
       {
          keycode = window.event.keyCode;
       }
       else if (e)
       {
          keycode = e.which;
       }
       else
       {
          return true;
       }
    
       if (keycode == 13)
       {
          myfield.form.submit();
          return false;
       }
       else
       {
          return true;
       }
    }
    </script>
  3. This last situation, which worked best for me also checks for the enter key using javascript, but instead of simply activating the form’s action, the function to be run is directly specified. Below, EmailTextBox is the last input TextBox element. Next, replace LoginButton with the ASP Submit Buttton. This script needs to go in the Page_Load section of the codebehind.
    EmailTextBox.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + LoginButton.UniqueID + "','')");

- EDIT JULY 16, 2008 -

See Form Submit On Enter in Visual Studio post for both the GUI and programmatic way to submit form using the enter key!

Create a Bootable USB Memory Stick

Nowadays, most computers come built without a 3.5″ floppy drive. And 99% of the time, the floppy drive just is not required anymore. USB Flash drives, otherwise known as memory sticks or thumb drives, have made the floppy disk obsolete. Therefore, booting from a memory stick is the next step.To create a memory stick, you will need to first download and extract (unzip) two files: HP USB Disk Format Tool and Windows 98 System Files. Follow the steps below:

  1. To create the bootable USB memory stick, install the HP USB Disk Format Tool.
  2. Insert your USB memory stick into a the computer.
  3. IMPORTANT: Backup files you wish to keep onto the local hard drive (as this process will delete all files on the memory stick).
  4. Start up the installed application from the HP USB Disk Format Tool.
  5. The device dropdown will automatically populate and identify your memory stick.
  6. Select Fat32 from the Filesystem dropdown.
  7. Check the box with “Create a DOS startup disk”.
  8. In the following textbox, browse to the location of the extracted files from the Windows 98 Systesm Files.
  9. Click the “Start” button.
  10. Click “Yes” to the following dialog box warning you of file deletion.
  11. Next, copy all extracted files from the Windows 98 System Files folder onto the USB memory stick to enable cd rom and mouse support.

Your USB memory stick is now bootable. If you wish to boot your computer from the USB key, be sure to set the proper settings in the BIOS.

Store Constant Variables in WebConfig

In a web application, there is sometimes a need to store a read only string accessible throughout the project without having to access a database. An easy way to achieve this end goal is to create a key in the web.config file.

An advantage to adding a key to the appSettings of the web.config file is that the key will be accessible to all files in your project. Secondly, if this key should ever change, you can edit the key’s value without the need to recompile! Therefore, the value is semi-constant in that it doesn’t change during execution, but can be easily altered between builds. The key is added within appSettings of configuration in the web.config file.

<configuration>
<appSettings>
</appSettings>
<configuration>

Next is a simple example of how this can be used. Below is the relevant code sample of the web.config file.

<configuration>
<appSettings>
<add key =”supportEmailGroup” value=”support@victorchen.info”/>
</appSettings>
<configuration>

To access the key named supportEmailGroup, use the following code below. In the example below, the supportEmailGroup value is accessed in the code behind.

string supportEmailAddress = ConfigurationSettings.AppSettings["supportEmailGroup"];

It is that easy. The web.config file can hold an arbitrary number of keys. I use the appSettings feature in every project. Some example of where this can be useful is pointing to file directory or connection strings.