Posts Tagged ‘How to set default button in ASP.Net’

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

June 23, 2011 5 comments

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" />
%d bloggers like this: