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
Asked
Active
Viewed 5.3k times
3
-
1These are client-side events, so, yes, you need JavaScript. – John Saunders Sep 21 '12 at 07:58
-
Yea you need to have javascript to do the needful. – Furqan Safdar Sep 21 '12 at 07:59
-
http://forums.asp.net/t/1334378.aspx/1 go with it – Gyan Chandra Srivastava Sep 21 '12 at 08:04
-
I think an answer with 78 up votes will do the job :) [http://stackoverflow.com/questions/45827/how-do-you-automatically-set-the-focus-to-a-textbox-when-a-web-page-loads][1] [1]: http://stackoverflow.com/questions/45827/how-do-you-automatically-set-the-focus-to-a-textbox-when-a-web-page-loads – Alex Sep 21 '12 at 08:18
2 Answers
10
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;
}

Yograj Gupta
- 9,811
- 3
- 29
- 48
-
document.getElementById('textbox2') would probably not work for a control of type asp:TextBox – Ozair Kafray Nov 09 '15 at 07:48
-
@OzairKafray document.getElementById('textbox2') will not work for a server side control, if it is on master page or on user control or on a page which uses master page. – Yograj Gupta Nov 09 '15 at 08:58
-
-
1@AltF4_ In that case, if your JS code is on aspx page, you can try ClientId property of that server control like `document.getElementById('<%=textbox1.ClientId%>').value`. – Yograj Gupta Oct 30 '17 at 07:10
0
try TextBox1.Focus() for getting the focus on textbox and for lost focus, take focus from this textbox1 to another or some hidden control.

Aishwarya Shiva
- 3,460
- 15
- 58
- 107