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?
                   
joan2006Asked:
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.

sasapopovicCommented:
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);
                        control.Attributes.Add("onkeydown",
                              clickbutton);
                  }
            }

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) {
            targetButton.click();
            return false;
      }
      return true;
}

Regards,
Sasa
0

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!!
0
cwbrandsdalCommented:
I wrote this article about using textbox OnKeyDown If you need to postback to server:

http://cwb.no/post/2008/03/OnKeyPress-and-OnKeyUp-AspNet-TextBox.aspx
0
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
ASP.NET

From novice to tech pro — start learning today.