Monday, April 26, 2010

Capturing Enter Key Press in Text Box ASP.NET Web Application

Copy This Java Script:

function test(e) {
e = e || window.event;
alert(e.keyCode);
if (e.keyCode == "13") {

document.getElementById("Text1").value = "13";
}
}




-----------------------------------------------------------------------------

In .aspx Page Add Particular Text box within div tag and place hidden input .

< div onkeydown="test(event)" >

< asp:textbox autopostback="true" id="TextBox1" ontextchanged="TextBox1_TextChanged" runat="server" > </asp:textbox >
<input  id="Text1" runat="server" type="text" />
</div>
<asp:textbox id="TextBox2" runat="server"></asp:textbox>


Also add the ontextchanged event in Text Box.

------------------------------------------------------------------------------

In .ascx.cs Page Within the TextChanged Event add follows .

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if( Text1.Value=="13")
{
Text1.Value = "";
//ENTER YOUR CODE
TextBox2.Text = "Enter Key";

}
}

No comments:

Post a Comment