Solution to bind TextBox carriage return event in asp.net


1. Bind all carriage returns on the page to the button


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. Set the button client event in the OnPreRender event


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

And we’re done. Reference article: //www.ofstack.com/article/27713.htm

Reference: 1. Bind all carriage returns on the page to a single button


<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. Different TextBox bind different Button


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

In the corresponding cs file // binding TextBox carriage return event 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 code:

<fieldset>
<legend id="LegendDetail" [ Check � � ]</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>

That’s the pattern. Press enter on textbox and call btnQuery_Click