asp.net中绑定TextBox回车事件的解决方法


1.将页面上的回车事件都绑定到按钮上
复制代码 代码如下:

function EnterTextBox(e)
{
var msie = (document.all) ? true : false;
var keycode;
if(!msie) keycode = window.event ? e.keyCode : e.which;
else keycode = e.keyCode;
//alert(keycode);
if(keycode==13 && document.getElementById('<%=this.txtSearch.ClientID%>').value != "")
{
//alert("test");
if(msie)
{
e.keyCode = 9;
e.returnValue = false;
}
document.getElementById('<%=this.btnSearch.ClientID%>').click();
}
}

2. 在OnPreRender事件中设定按钮客户端事件
复制代码 代码如下:

protected override void OnPreRender(EventArgs e)
{
txtSearch.Attributes.Add("onkeypress", "EnterTextBox(event);")
}

大功告成了。
参考文章:
http://www.phpstudy.net/article/27713.htm

原文参考:
1.将页面上所有回车事件都绑定到一个按钮上
复制代码 代码如下:

<HEAD>
<script language="javascript">
function EnterTextBox()
{
if(event.keyCode == 13 && document.all["TextBox1"].value != "")
{
event.keyCode = 9;
event.returnValue = false;
document.all["Button1"].click();
}
}
</script>
</HEAD>
<body onkeypress="return EnterTextBox()">

2.不同的TextBox绑定不同的Button
复制代码 代码如下:

<HEAD>
<script language="javascript">
function EnterTextBox(button)
{
if(event.keyCode == 13)
{
event.keyCode = 9;
event.returnValue = false;
document.all[button].click();
}
}
</script>
</HEAD>

在对应的cs文件中
//绑定TextBox回车事件
TextBoxPortOfDestination.Attributes.Add("onkeypress", "EnterTextBox('ButtonChoose')");
TextBoxItemName.Attributes.Add("onkeypress","EnterTextBox('ButtonAdd')");
TextBoxCost_PX.Attributes.Add("onkeypress","EnterTextBox('ButtonAdd')");
TextBoxCost_1X20.Attributes.Add("onkeypress","EnterTextBox('ButtonAdd')");
web代码:
复制代码 代码如下:

<fieldset>
<legend id="LegendDetail" [查詢條件]</legend>
<table>
<tr><td>
<asp:TextBox ID="TextBox 1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td >
<asp:Button ID="btn" runat="server" OnClick="btnQuery_Click"/></td>
</tr>
</table>
</fieldset>

是这样的模式。在textbox回车,调用btnQuery_Click
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3