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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joan2006Author Commented:
Thanks sasapovik!!
I wrote this article about using textbox OnKeyDown If you need to postback to server:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.