简体   繁体   English

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

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

I am not able to get the event GotFocus, LostFocus of a textbox while creating a website. 创建网站时,无法获取文本框的事件GotFocus,LostFocus。 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. 正如我在前面的问题中所问的那样,当焦点是winforms中另一个文本框的文本时,如何将一个文本框的值转换为另一个文本框。 I was able to work it done in windows form. 我能够在Windows窗体中完成它。 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? 但是,当我在网站上尝试相同的操作时,我无法获取这些事件。.....应该使用Java脚本来获取这些事件吗? Pelase help 请帮助

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). Windows控件中有TextBox的GotFocus,LostFocus事件,而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中,您将获得文本框(实际上是网页上的输入type =“ text”)的事件焦点和模糊效果,并且可以将其用于目的。

For setting an event handler, use on + event as event handler and provide the js code which to execute. 要设置事件处理程序,请使用on + event作为事件处理程序,并提供要执行的js代码。

like for blur event you should add attribute onblur and 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;
}

尝试使用TextBox1.Focus()以获得对文本框的焦点,并失去焦点,将焦点从此textbox1移至另一个或某些隐藏的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM