Auto Ad Code

Showing posts with label enter key. Show all posts
Showing posts with label enter key. Show all posts

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>

Thursday, June 17, 2010

Making a default button (For Enter Key)

Let me explain this using an example.
One makes a login page. Places "Login" and "Cancel" buttons below "User name" and "Password" Text boxes. He expects that whenever he will be pressing the Enter key while typing in User name or the password text box the "Login" button will be clicked, but sometimes this doesn't happen. Either Cancel gets clicked or any other button from page get clicked. Frustrated? Ok here is the solution for this

For this use an ASP Panel, place all controls withing that panel (don't worry this panel will not disturb the formatting of your page) and set the default button property of that panel to whatever button you want to be clicked whenever Enter key will be pressed.

For Example

<asp:panel runat="server" id="pnlMain" defaultbutton="btnSave">
<!-- All of your controls here -->
</asp:panel>


Now whenever one will press enter in any text box written in between the panel then btnSave will be clicked by default.