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

时间:2011-05-10 09:44:54

标签: c# javascript asp.net

<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点击事件

7 个答案:

答案 0 :(得分: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>

希望这会对你有帮助......

答案 1 :(得分: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>

答案 2 :(得分:3)

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

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

并且像文章所说:&#34;决定哪种解决方案最适合您的需求&#34;

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

上面提到的链接,讨论了两种捕获&#34;输入密钥的方法。事件:

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的属性。你设定   此属性为您要单击的按钮的按钮ID   输入按键事件。所以Panel内的任何文本框都会   将其Enter键按到DefaultButton中设置的按钮   专家小组的财产

答案 3 :(得分: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!

答案 4 :(得分: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" />

答案 5 :(得分:-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

希望这个帮助

答案 6 :(得分:-3)

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

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