Auto Ad Code

Monday, July 12, 2010

Default Button (Enter Key)

Many of us faces the issue that while typing in a textbox we press Enter and expect a button named "A" to be clicked but this doesn't happens and button "B" gets clicked.
For example you have provided a Textbox for Search and a button is given with that textbox so that when that button will be clicked your code will try to search in database for whatever written in Textbox. You expect that when user will press enter while typing in Textbox then search button will be clicked by default but this doesn't happen and instead soneother button (like login etc) get clicked. Pretty much annoyiong is this and even tab sequence don't work here.
Whats the solution?
Its pretty much easy which is to add yout HTML code for that textbox and button withing a <asp:Panel. There is a property named "DefaultButton" for <asp:Panel. You can write ID of your button in the "DefaultButton" property of <asp:Panel. Now whenever Enter key will be pressed in any textbox contained in that <asp:Panel then that button will be clicked whose ID is given against "DefaultButton" property of <asp:Panel.

Sample code
<asp:Panel runat="server" ID="pnlChild" DefaultButton="ButtonID">
    YOUR HTML CODE Including the Button to be Clicked
</asp:Panel>

1 comment: