C# Asp.net 中的 TextBox KeyDown 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12072281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextBox KeyDown Event in Asp.net
提问by Shashank
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.
我正在尝试在 Asp.net 中像 windows 应用程序一样找到 KeyDown 事件.. 在文本框中按 Enter 后我有几个 TextBox 我想专注于保存按钮我如何实现它。
采纳答案by Sandeep Tawaniya
Yes, you can do this! You can use asp panel for it.
是的,你可以这样做!您可以使用 asp 面板。
Paste you textbox and button html code inside asp panel and than set panel's property of DefaultButtonto your button name.
将您的文本框和按钮 html 代码粘贴到 asp 面板中,然后将面板的属性设置DefaultButton为您的按钮名称。
回答by Dan Puzey
You can't do this from "ASP.NET" code (that is, C#) because the TextBoxdoesn'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.
您不能从“ASP.NET”代码(即 C#)执行此操作,因为TextBox当用户与其交互时,服务器上不存在该代码。您需要做的是使用 JavaScript 连接客户端行为,以响应用户浏览器中引发的文本框事件。
回答by perilbrain
It is suggested to use javascript for this purpose.
为此,建议使用 javascript。
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)");
}
回答by Manikandan Sigamani
You can use DefaultButton option. or You can use JavaScript functions for OnKeyDown function.
您可以使用 DefaultButton 选项。或者 您可以将 JavaScript 函数用于 OnKeyDown 函数。

