4

I have a master page with a search box and button at the top. This search functionality is taking over the "enter" key for all my web forms that use this master page. That is, if I have a login page that uses this master page and the user enters in their username/password and hits "enter", instead of logging in the user the system performs a search.

What's the best way to set up the default submit buttons. I could wrap everything in asp Panels and set the DefaultButton property, but that seems a bit tedious. Is there a better way?

5 Answers 5

3

use defaultbutton property of form or panel

<form defaultbutton=“button1” runat=“server”>    
    <asp:button id=“button1” text=“Same Page” runat=“server”/>    
    <asp:panel defaultbutton=“button2” runat=“server”>    
       <asp:textbox id=“foo” runat=“server”/>    
       <asp:button id=“button2” runat=“server”/>    
    </asp:panel>    
</form>
2
  • 2
    The OP stated this he was aware of this option but wanted to know if there is a better way. I am trying to solve this problem on a website with 100+ pages with forms. It would be too tedious to have to add this code to every single inner page. Is there something we can do at the master page level to tell the search button 'never assume this is the default button'?
    – Jesse Webb
    Apr 8, 2011 at 15:28
  • -1 Because it does not answer the question or help answer the question.
    – Lisa
    Aug 10, 2011 at 4:12
2

As you have discovered default buttons can cause issues when there is more than one button on a page. I would take the Geeks suggestion but simplify it by removing the setfocus client script and extend it by adding the keydown event to both the search textbox and the login textboxes such that the enter key fires the correct button depending on if your user is using the search box or the log in box, or any other textbox you want to add the javascript to.

You can set focus on loading the page (in code behind if you like) to save the user some mouse work or tabbing if there is a sensible control for the user to start at, but otherwise the control the user is interacting with should determine the flow of the page and what the enter key does.

0

set focus on the text box ,

Page.RegisterStartupScript("SetFocus", "< script >document.getElementById('" + TextBox1.ClientID + "').focus();< /script >");

and then

In the keydown event for the last textbox you can do something like this:

If e.KeyCode = Keys.Enter Then Me.Button1.Select() End If

0

Rather than using javascript to manipulate the page you could put the search box and the submit button into an IFRAME on the master page. If the focus is in the iframe clicking will submit the search form, if the user is on your main form within the page they will submit the normal page.

The <iframe> src attribute points to a little self contained aspx page holding your text box and submit button which redirects to your search results form.

0

This is what we tried on our project which seemed to work. I changed the Search asp:Button to be an asp:LinkButton. I then added some CSS style to give it a background image to make it look like a button. LinkButtons are apparently not used by the page for determining which Button is the default action when pressing Enter. The sweet part was that LinkButton can still have click actions associated with them so I didn't have to change any of my code-behind stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.