Solved

DropDownList on enter

Posted on 2004-07-31
4
601 Views
Last Modified: 2006-11-17
When I type a letter on a dropdownlist it would scroll down to the first word of the letter, how do I add the functionality to submit the form when I press enter while having focus on the dropdownlist. Basically adding an event handler that would trigger an onEnter event on a dropdownlist.


0
Comment
Question by:vinny45
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 11686153
Just add an onkeydown javascript like this

<html>
<head>
      <script language="Javascript">
      function CheckForEnterKey()
      {
         if( event.keyCode == 13 )
         {
            document.forms[0].submit();
         }
      }      
      </script>
</head>
      <body>
            <form id="form1" action="http://www.google.com" method="post">
                  <select id="myDDL" onkeydown="CheckForEnterKey()">
                        <option value="1">Abc</option>
                        <option value="2">BAbc</option>
                        <option value="3">BBAbc</option>
                        <option value="4">Cabc</option>
                  </select>
            </form>
      </body>
</html>

keyCode is 13 for the enter key.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11686209
0
 

Author Comment

by:vinny45
ID: 11686372
oh i'm sorry, I'm meant asp.net dropdownlist control, Aesros, will you're ariticle still work with web controls?
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 500 total points
ID: 11686519
The code I posted was just to show you the Javascript, you can register the javascript functon in you asp.net code. If you put it in the page_load code you could write

string strFunction = "<script language="Javascript"> function CheckForEnterKey() {  if( event.keyCode == 13 ) ";
strFunction +=" {   document.forms[0].submit(); } }  </script>";

RegisterStartupScript("EnterKeyFunction", strFunction);

then in the html section you would and the onkeydown="CheckForEnterKey()" code or if you are doing strictly in C# code then after the RegisterStartupScript function above you could call the AddAttribute method of the control

ddl.AddAttribute("onkeydown", "CheckForEnterKey()");

that should work.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question