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

前端 未结 2 1363
南方客
南方客 2021-01-20 13:27

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 t

相关标签:
2条回答
  • 2021-01-20 13:51

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

    0 讨论(0)
  • 2021-01-20 14:09

    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;
    }
    
    0 讨论(0)
提交回复
热议问题