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" r开发者_运维问答unat="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();
}
});
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
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 %>")
精彩评论