javascript 在aspx中如何在源cs文件中定义的按钮单击事件时在文本框上按“Enter”时触发按钮单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21276190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In aspx how to trigger button click event when press "Enter" on a textbox while the button click event defined in source cs file
提问by Jerry
While, I want to trigger btnSearchSuiteGroup_Click
event when pressing "enter" on txtSuiteGroupName
which described in aspx, code below:
同时,我想btnSearchSuiteGroup_Click
在按txtSuiteGroupName
aspx 中描述的“输入”时触发事件,代码如下:
<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?
浏览网页时,文本框上的按键无法启动按钮点击事件,代码有问题吗?
回答by Nitin Varpe
If you use Panel
you would not need to use any javascript function. You can specify default button
Id for panel like below
如果您使用Panel
,则不需要使用任何 javascript 函数。您可以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
!
当然,您可以panels
在单个页面上设置多个,以便buttons
为单独的panels
!
For More On Panel.DefaultButton Property
回答by Nishanth Reddy
I suggest the following method: 1. Create a function 'ButtonClick' and put all the code in 'btnSearchSuiteGroup_Click' function into it. 2. Add 'onkeypress' event for the textbox 'txtSuiteGroupName' by looking at this thread3. Whenever the above event is fired, see if the entered key is 'Enter'. 4. If the key is 'Enter' key, call the function 'ButtonClick'.
我建议采用以下方法: 1. 创建一个函数“ButtonClick”,并将“btnSearchSuiteGroup_Click”函数中的所有代码放入其中。2. 通过查看此线程为文本框 'txtSuiteGroupName' 添加 'onkeypress' 事件 3. 每当触发上述事件时,查看输入的键是否为 'Enter'。4. 如果键是“Enter”键,则调用函数“ButtonClick”。