简体   繁体   中英

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. 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 events for TextBox are in Windows Control but for WebControls, You will not get these, Instead of you should try clientside scripting (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.

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

like for blur event you should add attribute onblur and for focus add attribute onfocus

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

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

in javascript

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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