简体   繁体   English

在aspx中如何在源cs文件中定义的按钮单击事件时按下文本框上的“Enter”时触发按钮单击事件

[英]In aspx how to trigger button click event when press “Enter” on a textbox while the button click event defined in source cs file

While, I want to trigger btnSearchSuiteGroup_Click event when pressing "enter" on txtSuiteGroupName which described in aspx, code below: 虽然,我想在aspx中描述的txtSuiteGroupName上按“enter”时触发btnSearchSuiteGroup_Click事件,代码如下:

<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD" onkeypress="return searchKeyPress(event)"></asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search"  CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
<script type="text/javascript">
    function searchKeyPress(e) {

        // look for window.event in case event isn't passed in
        if (typeof e == 'undefined' && window.event) { e = window.event; }
        if (e.keyCode == 13) {
            document.getElementById('<%=btnSearchSuiteGroup.ClientID%>').click();
        }
    }
</script>

While, the btnSearchSuiteGroup_Click is defined in the source cs file: 同时, btnSearchSuiteGroup_Click在源cs文件中定义:

protected void btnSearchSuiteGroup_Click(object sender, EventArgs e)
{
    this.LinqDataSource1.WhereParameters["SuiteGroupName"].DefaultValue = this.txtSuiteGroupName.Text;
    this.GridView1.DataBind();
    if (GridView1.Rows.Count == 0)
        Response.Write("<script language='javascript'>window.alert('No record found!')</script>");
}

When I browse the website, the keypress on the textbox cannot initiate the button click event, anything wrong in the code? 当我浏览网站时,文本框上的按键无法启动按钮点击事件,代码中有什么问题?

If you use Panel you would not need to use any javascript function. 如果您使用Panel ,则不需要使用任何javascript函数。 You can specify default button Id for panel like below 您可以为面板指定默认button ID,如下所示

    <asp:Panel runat="server" DefaultButton="btnSearchSuiteGroup">
       <asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD">          
       </asp:TextBox>
        <asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search"  CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
        </asp:Button>
    </asp:Panel>

OfCourse you can have Multiple panels on single page for assigning different default buttons for separate panels ! OfCourse你可以在单页上有多个panels ,为不同的panels分配不同的默认buttons

For More On Panel.DefaultButton Property 有关Panel.DefaultButton属性的更多信息

I suggest the following method: 1. Create a function 'ButtonClick' and put all the code in 'btnSearchSuiteGroup_Click' function into it. 我建议使用以下方法:1。创建一个函数'ButtonClick'并将'btnSearchSuiteGroup_Click'函数中的所有代码放入其中。 2. Add 'onkeypress' event for the textbox 'txtSuiteGroupName' by looking at this thread 3. Whenever the above event is fired, see if the entered key is 'Enter'. 2.通过查看此线程 3为文本框'txtSuiteGroupName'添加'onkeypress'事件。每当触发上述事件时,查看输入的键是否为'Enter'。 4. If the key is 'Enter' key, call the function 'ButtonClick'. 4.如果键是“Enter”键,则调用“ButtonClick”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户在文本框中按Enter键时,执行按钮单击事件 - Perform Button click event when user press Enter key in Textbox 如何在c#中按Enter键触发按钮点击事件 - How to trigger button click event on pressing enter button in c# 如何在asp.net中在textchanged事件上输入文本框时避免按钮单击事件 - how to avoid button click event when enter the textbox on textchanged event in asp .Net 如何将按钮单击事件更改为输入事件? - How to change button click event into enter event? 如何在WPF用户控件中触发单击事件(带按钮的文本框)? - How to Trigger Click Event in WPF User control (Textbox with Button)? 当基于具有焦点的文本框按下Enter键时,如何触发Button Click事件? - How can I fire a Button Click event when Enter Key is pressed based on what textbox has focus? 执行线程时触发按钮单击事件 - Trigger button click event while executing thread 模拟 Enter Key Press 从按钮的 Click 事件上发送? - Simulating Enter Key Press sending from a Button on it's Click Event? 在按钮单击事件上启用文本框 - Enabling textbox on button click event 当用户在Jquery中按文本框输入时,如何只调用一个按钮单击功能? - How to call only one button click function when user press enter from Textbox in Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM