TextBox Onkeypress

Hi all

I am using an asp textbox.

<asp:TextBox ID="txtID" runat="server" Font-Size="8pt" Width="69px" AutoPostBack="True" TabIndex="1"  LEFT: 86px; " ></asp:TextBox>

<asp:Button ID="btnSearch" runat="server" Font-Size="8pt" Text="Search" OnClick="btnSearch_Click" TabIndex="2" />

I need for my search button click event to fire when              
a user is typing in the textbox and they hit the ENTER key.  

How can i do this using the asp textbox?
Who is Participating?
sasapopovicConnect With a Mentor Commented:
Hi joan,

Here is what you should do:
1. First remove the AutoPostBack="true" from your text box control.
2. Add next method to one of your utlity classes (or to code behind of your page):
            /// <summary>
            /// Add an attribute to a control to enable that &lt;enter&gt; posts the page
            /// This method is used in pages with multiple input fields and buttons where each field should be related to one button.
            /// </summary>
            /// <param name="control">Input fieldcontrol for which the &lt;enter&gt; function should be enabled</param>
            /// <param name="targetButtonClientId">Id of the button that is to be submitted after &lt;enter&gt;</param>
            public static void SetTextboxEnterTarget(System.Web.UI.WebControls.WebControl control, string targetButtonClientId) {
                  if(control != null){
                        string clickbutton =
                              string.Format("return Clickbutton(event,{0});", targetButtonClientId);

3. In Page_Load of your page add next line:
SetTextboxEnterTarget(txtID, btnSearch.ClientID);
4. Add next JavaScript function to your page (you can add it to <script> block in head of your page or add it to a JavaScript file that you included it into page):

function Clickbutton(e, targetButton) {
      if (e.keyCode == 13) {
            return false;
      return true;

joan2006Author Commented:
Thanks sasapovik!!
I wrote this article about using textbox OnKeyDown If you need to postback to server:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.