views:

1030

answers:

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?

A: 

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

Samiksha
+5  A: 

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>
Ramesh Soni
+1  A: 

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.

iamdudley
A: 

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.

Steve Davies