Results 1 to 3 of 3

Thread: How find in which text box is focused in Asp.Net

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    241

    Thumbs up How find in which text box is focused in Asp.Net

    Hi

    I have four text box. In my application the 2nd text box is focused. while i click

    the button the 2nd text box is focused. But i dont know while i click the button

    In which text box is focused. How i find that while i click the button in which

    text box is focussed. I hope for ur's reply friends.

    Thanks
    Failing to plan is Planning to fail

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: How find in which text box is focused in Asp.Net

    The close I could get is using some JavaScript. Add a script for onfocus event of TextBox, and there pass the ID of selected or just focused textbox to a hiddenfield.

    When clicking the button, get the value from this hiddenfield which contains the current focussed textbox.
    HTML Code:
    <head>
    <script type="text/javascript">
    function btnclick()
    {
    	alert(document.getElementById('hid').value);
    }
    
    function fcs(txt)
    {
    document.getElementById('hid').value = txt;
    }
    </script>
    </head>
    <body>
    <input type="text" id="txt1" onfocus="fcs(this.id);" /><br />
    <input type="text" id="txt2" onfocus="fcs(this.id);" /><br />
    <input type="text" id="txt3" onfocus="fcs(this.id);" /><br />
    <input type="text" id="txt4" onfocus="fcs(this.id);" /><br /><br />
    <input type="button" id="btn" onclick="btnclick()" value="Check" />
    <input type="hidden" id="hid" />
    </body>
    Show Appreciation. Rate Posts.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How find in which text box is focused in Asp.Net

    Just a slight modification. Because the controls may be dynamic and of an unknown quantiy, you may want to attach the javascript via DOM.

    Loop through getElementsByTagName ('*'), for each element, set

    element.onfocus = fcs(this.id);

    Do this in the body onload (javascript).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width