I am trying to find KeyDown event in Asp.net like windows application.. i have several TextBox after press enter inside textbox i want to focus on save button how can i achieve it.
4 Answers
You can't do this from "ASP.NET" code (that is, C#) because the TextBox doesn't exist on the server when the user is interacting with it. What you need to do instead is wire up client-side behaviour using JavaScript that can respond to textbox events raised in the user's browser.
1 Comment
specstr
Only if all the answers in this forum had informative explanations like this one had: "... because the TextBox doesn't exist on the server when the user is interacting with it."
It is suggested to use javascript for this purpose.
Example:-
<script type="text/javascript" language="javascript">
function handleEnter (obj, event)
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13)
{
document.getElementById(obj).click();
return false;
}
else {
return true;
}
}
</script>
Relevant codes
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onkeypress", "return handleEnter('" + Button1.ClientID + "', event)");
}
Comments
Yes, you can do this! You can use asp panel for it.
Paste you textbox and button html code inside asp panel and than set panel's property of DefaultButton to your button name.
1 Comment
Rahul Uttarkar
its Working for only Enter Key only.. How about other keys...?