当用户在文本框中按Enter键时,执行按钮单击事件

问题描述 投票:36回答:7
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>

当用户在Button1中按Enter key时,我必须执行Textbox1点击事件

c# javascript asp.net
7个回答
17
投票

在aspx页面加载事件中,将onkeypress添加到框中。

this.TextBox1.Attributes.Add(
    "onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");

然后添加此javascript以评估按键,如果是“输入”,则单击右键。

<script>
    function button_click(objTextBox,objBtnID)
    {
        if(window.event.keyCode==13)
        {
            document.getElementById(objBtnID).focus();
            document.getElementById(objBtnID).click();
        }
    }
</script>

82
投票

将表单放在asp.net面板控件中,并使用按钮Id设置其defaultButton属性。请参阅以下代码:

  <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
         <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
             </ContentTemplate>
          </asp:UpdatePanel>
    </asp:Panel>

希望对你有帮助...


3
投票

Codeproject有一个完整的解决方案:

http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click

并且像文章说:“决定哪种解决方案最符合您的需求”

===================编辑答案============================

上面提到的链接,讨论了捕获“Enter Key”事件的两种方法:

Javascript(将onKeyPress事件绑定到对象并创建一个javascript函数来检查按下了哪个键并执行逻辑)

_Page_Load代码背后:_

 //Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
{
   txtboxFirstName.Attributes.Add("onKeyPress", 
                   "doClick('" + btnSearch.ClientID + "',event)");
   txtboxLastName.Attributes.Add("onKeyPress", 
                  "doClick('" + btnSearch.ClientID + "',event)");
}

Javascript代码:

<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
    {
        //the purpose of this function is to allow the enter key to 
        //point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox

        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }
</SCRIPT>

面板控制

<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
    <asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
</asp:Panel>

引用:

请注意,Panel标记有一个名为DefaultButton的属性。您将此属性设置为要在Enter键按下事件上单击的按钮的按钮ID。因此,面板内的任何文本框都会将其Enter键按下,直到Panel的DefaultButton属性中设置的按钮


1
投票

仅在html代码中,添加包含页面控件的面板。在面板内,添加一行DefaultButton =“buttonNameThatClicksAtEnter”。请参阅下面的示例,不需要其他任何内容。

<asp:Panel runat="server" DefaultButton="Button1"> //add this!
  //here goes all the page controls and the trigger button
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</asp:Panel> //and this too!

0
投票

你可以用javascript / jquery做到这一点:

<script>
    function runScript(e) {
        if (e.keyCode == 13) {
            $("#myButton").click(); //jquery
            document.getElementById("myButton").click(); //javascript
        }
    }
</script>

<asp:textbox id="txtUsername" runat="server" onkeypress="return runScript(event)" />

<asp:LinkButton id="myButton" text="Login" runat="server" />

-2
投票

你可以试试:

在HTML中:

    <asp:TextBox ID="TextBox1" runat="server" onKeyDown="submitButton(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

和javascript:

function submitButton(event) {
        if (event.which == 13) {
            $('#Button1').trigger('click');
        }
    }

代码背后:

protected void Button1_Click(object sender, EventArgs e)
{
        //load data and fill to gridview
} // fixed the function view for users

希望这有帮助


-3
投票

使用Jquery或其他东西就是例子

http://riderdesign.com/articles/Check-username-availability-with-JQuery-and-ASP.NET.aspx我希望我会帮助你更多

© www.soinside.com 2019 - 2024. All rights reserved.