how to set a default 'enter' on a certain button

一曲冷凌霜 提交于 2019-11-27 08:10:16

The easiest way is to put the fields and button inside of a Panel and set the default button to the button you want to be activated on enter.

<asp:Panel ID="p" runat="server" DefaultButton="myButton">
  <%-- Text boxes here --%>
  <asp:Button ID="myButton" runat="server" />
</asp:Panel>
Lester

if you need to do it from code, use

Me.Form.DefaultButton = Me.btn.UniqueID

Where btn is your button control.

You can use the DefaultButton property on either a server-side form control or Panel control. In your case, group the controls together in a Panel that should fire off the same button:

<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
    ...
    <asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
    ...
    <asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>

You can now use UseSubmitBehavior property to disable all the buttons you don't want to fire when hitting submit (check out the documentation for more info)

    <asp:Button ID="BtnNotToFIre" runat="server" Text="Search" UseSubmitBehavior="false" />
$(document).ready(function(){
    document.getElementById("text_box_id")
    .addEventListener("keyup", function(event) {
    event.preventDefault();
    if (event.keyCode === 13) {
        document.getElementById("button_id").click();
    }
    });
});

Microsoft say:

<form id="Form1"
    defaultbutton="SubmitButton"
    defaultfocus="TextBox1"
    runat="server">

enter link description here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!