0

How can I set default button after text is entered into a text box. This is what I have so far. But it doesn't work

<td>
                    <asp:Label ID="displayrowLabel" runat="server" Text="# of Rows Displayed:"></asp:Label>
                    <asp:TextBox ID="displayRowQuery" runat="server"></asp:TextBox>
                    <asp:Button ID="displayRowButton" runat="server" Text="Click" OnClick="ddlPageItems_SelectedIndexChanged" />
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="displayRowQuery"  ValidationExpression="[1-9][0-9]*"  ErrorMessage="Wrong Input" />
                </td>               
            </tr>
        </table>            
    </PagerTemplate>

I tried adding some jquery to handle this:

<script type="text/javascript">
    $("displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("displayRowButton").click();
        }
    });

reddevil
  • 47
  • 1
  • 9

2 Answers2

2

You can also set a default button for an asp:panel if you have a group of controls you need a default button for.

Basically:

<asp:Panel ID="aPanel" runat="server" DefaultButton="btnSubmit2">

    <!-- Some Controls Here -->

    <asp:Button UseSubmitBehavior="true" ID="btnSubmit2" Text="Submit" runat="server" onclick="btnSubmit2_Click" />

</asp:Panel>

http://www.aspnettutorials.com/tutorials/controls/defaultbutton-panel-aspnet.aspx

Jon P
  • 19,442
  • 8
  • 49
  • 72
0

You're missing the "#" in your id selector:

    $("#displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#displayRowButton").click();
        }
    });

Edit: As @Nicolás has pointed out, you may have to substitute the id selectors with ClientId like this:

$("#displayRowQuery") => $("#<% = displayRowQuery.ClientId %>") 
$("#displayRowButton") >= $("#<% = displayRowButton.ClientId %>")
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 2
    I wouldn't like to add an answe because this is fair enought. however you may need to add some trick to convert the server-side ID to client-side id ... `$("#displayRowQuery") => $("#<% = displayRowQuery.ClientId %>")`
    `$("#displayRowButton") >= $("#<% = displayRowButton.ClientId %>")`
    – Nicolás Aug 09 '11 at 00:04
  • Where do I add this code? I have a search page with one textbox and one submit button. I add this code and it works fine.......... protected void Page_Load(object sender, EventArgs e) { Page.Form.DefaultButton = submitQuery.UniqueID; Page.Form.DefaultFocus = searchQuery.ClientID; } ............... I tried doing the same for displayRow..and it didn't work out. – reddevil Aug 09 '11 at 16:16