如何使用 C# 获取 Webforms 中文本框的 GotFocus、LostFocus 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12526429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:34:13  来源:igfitidea点击:

How to get GotFocus, LostFocus event of a textbox in Webforms using C#

c#asp.net

提问by Running Rabbit

I am not able to get the event GotFocus, LostFocus of a textbox while creating a website. I just wanted. As I have asked earlier in my question how to get the value of one textbox into another textbox when focus is text to the other textbox in winforms. I was able to work it done in windows form. But, when I try the same in a website, I am not able to get these events their.....Should Java script be used to get these events? Pelase help

在创建网站时,我无法获得文本框的 GotFocus、LostFocus 事件。我只是想。正如我之前在我的问题中所问的那样,当焦点是 winforms 中另一个文本框的文本时,如何将一个文本框的值放入另一个文本框。我能够以 Windows 形式完成它。但是,当我在网站上尝试相同的操作时,我无法将这些事件.....应该使用 Java 脚本来获取这些事件吗?请帮忙

采纳答案by Yograj Gupta

GotFocus, LostFocus events for TextBox are in Windows Control but for WebControls, You will not get these, Instead of you should try clientside scripting (Javascript).

文本框的 GotFocus、LostFocus 事件在 Windows Control 中,但对于 WebControls,您不会得到这些,而您应该尝试客户端脚本 (Javascript)。

In javascript you will get the event focus and blur for a textbox (which is actually a input type="text" on web page) , and you can use these for your purpose.

在javascript中,您将获得文本框的事件焦点和模糊(实际上是网页上的输入类型=“文本”),您可以将它们用于您的目的。

For setting an event handler, use on + eventas event handler and provide the js code which to execute.

要设置事件处理程序,请on + event用作事件处理程序并提供要执行的 js 代码。

like for blur event you should add attribute onblurand for focus add attribute onfocus

像模糊事件你应该添加属性onblur和焦点添加属性onfocus

In Javascript you can try, if your aspx has TextBox as

在 Javascript 中你可以试试,如果你的 aspx 有 TextBox 作为

<asp:TextBox runat="server" id="textbox1" onblur="SetTextInTextBox2()" />
<asp:TextBox runat="server" id="textbox2" onfocus="SetTextInTextBox2()" />

in javascript

在 JavaScript 中

function SetTextInTextBox2()
{
    document.getElementById('textbox2').value = document.getElementById('textbox1').value;
}

回答by Aishwarya Shiva

try TextBox1.Focus() for getting the focus on textbox and for lost focus, take focus from this textbox1 to another or some hidden control.

尝试 TextBox1.Focus() 以获取对文本框的焦点和失去焦点,将焦点从此 textbox1 转移到另一个或某些隐藏控件。