Home > DotNet > Press enter for search in asp.net / Call button click event on press enter button in ASP.Net

Press enter for search in asp.net / Call button click event on press enter button in ASP.Net

How to call code behind function on press enter in ASP.Net? 

In ASP.Net application we have to call a server side function on press enter from any text box. Suppose we have a textbox for search items and we need to call search function once the user enter the keywords in the text box and press enter button. Here we can use javascript or jquery to call server side function in ASP.Net. But it is little bit overhead to developer to achieve it. 

How to call search funtion when press enter button in search texbox?

To call search function when a user press enter button from search box, we can set default button for a asp panel to the search button in our aspx page.

Steps to call server side function on enter key press / Accessing A Code Behind Function On Keypress Event

  1. Create a aspx page and create a aspx panel.
  2. Create a Search text box (txtSearchBox) and search button (btnSearch) inside the asp panel control.
  3. Set default button for the asp panel to btnSearchBox button.
  4. On button click event call search funtion in the code behind.
  5. The if we press enter from search textbox, it wwill call button click event in the code behind. 

ASPX Page 

<asp:Panel ID="Panel1" DefaultButton="btnSearch" runat="server">
<table width="100%" cellpadding="0" cellspacing="0" style="padding-top: 15px;">
  <td valign="middle" style="width: 250px;">
   What are you looking for?
  <td align="right" style="width: 300px;">
   <asp:TextBox ID="txtSearch" runat="server" CssClass="qkSearchTextBox" />
  <td align="left">
   <asp:Button ID="btnSearch" runat="server" Text="Search"
   CssClass="searchButton" OnClick="btnSearch_Click1" />
  1. Praveen
    February 21, 2012 at 9:12 pm

    if (event.keyCode == 13) __doPostBack(‘ContentPlaceHolder1_btnSearch’,’OnClick’);

  2. March 30, 2012 at 3:45 pm

    how i capture enterkey press

  3. March 30, 2012 at 3:45 pm

    how can i capture enter key pressed event…..??????

  4. March 30, 2012 at 4:51 pm

    Hi Tahir,

    Inorder to get enter key event, 1. In the textbox, onKeyDown and onMouseDown event you just call function ‘return getEnterKey(even)’

    2. In javascript, create a function

    function getEnterKey(e){ if(e.keyCode == 13){ alert(‘pressed enter key’) return false; } }

    If you have any doubt, feel free to post quries. Thanks

  5. May 22, 2012 at 11:38 pm

    Valuable information. Lucky me I found your web site by accident, and I’m stunned why this coincidence did not came about earlier! I bookmarked it.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: