[英]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.