jquerytextboxdefaultbuttonaspbutton

how to make asp:button a default button on entering text to asp:textbox


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();
        }
    });


Solution

  • 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