Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding control focus

    Hi,
    How can I find out which control has focus on a postback, so that when a page is re-served to the user I can set the focus to the that control?
    V

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Control? Postback? Care to elaborate?

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hum, sorry... It's asp.net terminology...
    Hum, basically, I have several input fields that submit a form to the server through javascript. What I'm looking for, and I hope javascript can do this, is that I need some way of getting the id of the field that has focus prior to the submit, so that I can reset it when I repost the page to the user.
    Thanks,
    V

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do something like this:

    Code:
    <input type="hidden" name="focusedField">
    
    <input .... onfocus="this.form.focusedField.value = this.name;">

    This would place the name of the last field to receive the focus into a hidden form field which your server side page could receive and feed into the response and use to set the focus to that field on the reply.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Last edited by whammy; 09-28-2002 at 02:45 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Roy,
    I had already tried that, but it didn't work....
    I think it was because the inputs were being submitted to the server on an onchange event, while I setting the hidden input with an onfocus event. What resulted was that both events were trigged at the same time, and the hidden input field never got updated with the new id of the focused control.
    So my problem is that I need to figure out which control has focus on the onchange event. So that I don't get a racing condition.

    Whammy, I checked that site, but I found nothing about setting focus after a postback, perhaps that was a wrong link???
    V

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about that - it should be:

    http://www.codetoad.com/asp.net/aspnetcontrols7.asp

    ...it's the first line that is of particular note. This is a hidden control called __VIEWSTATE, whose value is an encoded representation of the overall state of the form (as it was when last submitted). This is used by ASP.NET to keep track of all the server control settings from one page refresh to another – otherwise, our drop-down listbox would revert to a static default setting every time we submitted a value.

    It may not be immediately obvious how useful this can be – consider a registration form in which you have to enter a full set of personal details. If you forget to fill in a required field, and then submit the form, you may well be prompted with the same form again. Perhaps the field you missed will be highlighted, but unless the page has been very carefully coded (or is running on a sophisticated technology like ASP.NET), all the data you just entered will have to be typed in again. Thanks to __VIEWSTATE, all that data is automatically persisted through to the refreshed page, and we (as developers) haven't even to raise a finger!
    Last edited by whammy; 09-29-2002 at 02:06 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    As per Roy Sinclair:

    <html>
    <head>
    <title>untitled</title>
    </head>
    <body>
    <form>
    <input type="hidden" name="focusedField">
    <input name="testcontrol1" type="text"
    onfocus="focusedField.value=this.name"
    onchange="this.form.submit()">
    <input name="testcontrol2" type="text"
    onfocus="focusedField.value=this.name"
    onchange="this.form.submit()">
    </form>
    </body>
    </html>

    Browser (OS) events don't 'race', they're fired in a predictable sequence. A change event can't be fired on a form control unless the control has been focused first (and changed!).

    whammy's solution looks like it might be more robust.

  • #9
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    adios, then I don't know what's happening with the events but your code doesn't work also....
    All I get is the id of the server that created the submit....
    For instance, if I click on testcontrol1, type some stuff and change focus into testcontrol2, on the server the hidden field focusedField will have the name of the first focused control (testcontrol1) and not the last, which is not what I want.
    Whammy, the viewstate doesn't save focus information.... There's one thing that does in ASP.NET called smartnavigation (i believe it uses an iframe to do this), but again that only puts the focus on the control that created the submit event, which is what I'm trying to avoid.
    I don't know, is what I'm looking for hopeless?????
    V

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I must have misunderstood your post... why do you want to submit the form each time anything is changed (or do you)?!?

    Usually everything on a form will be validated once the form is submitted, especially using a server-side language... and then you would of course return focus to the first invalid field...

    Anyway, along the exact same lines as adios and Roy Sinclair, this works just fine for me...

    Code:
    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    <!--
    function savefoc(fldname){
         // alert(fldname);
         document.form1.savefocus.value = fldname;
    }
    // -->
    </script>
    </head>
    
    <body>
    
    <form name="form1" action="savefocus.htm" method="get">
         <input type="text" name="field1" onfocus="savefoc(this.name)" /><br />
         <input type="text" name="field2" onfocus="savefoc(this.name)" /><br />
         <input type="text" name="field3" onfocus="savefoc(this.name)" /><br />
         <input type="hidden" name="savefocus" value="" />
         <input type="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    And, as adios said, the focus MUST be on the field before it is possible for an onchange event to fire... which is why this modification also works with no problems:

    Code:
    <form name="form1" action="savefocus.htm" method="get">
         <input type="text" name="field1" onfocus="savefoc(this.name)" onchange="this.form.submit()" /><br />
         <input type="text" name="field2" onfocus="savefoc(this.name)" onchange="this.form.submit()" /><br />
         <input type="text" name="field3" onfocus="savefoc(this.name)" onchange="this.form.submit()" /><br />
         <input type="hidden" name="savefocus" value="" />
         <input type="submit" value="Submit" />
    </form>
    I can only assume you have some other error happening that is not directly related to this problem, or a syntax error in your scripting.

    Last edited by whammy; 09-29-2002 at 08:40 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Not sure I get what you're up to, either, but, it's your thread:
    Code:
    <html> 
    <head> 
    <title>untitled</title>
    </head> 
    <body> 
    <form> 
    <input type="hidden" name="focusedField"> 
    <input name="testcontrol1" type="text" 
    onfocus="if(this.form.changed){focusedField.value=this.name;this.form.submit()}" 
    onchange="this.form.changed=true"> 
    <input name="testcontrol2" type="text" 
    onfocus="if(this.form.changed){focusedField.value=this.name;this.form.submit()}" 
    onchange="this.form.changed=true">
    <input name="testcontrol3" type="text" 
    onfocus="if(this.form.changed){focusedField.value=this.name;this.form.submit()}" 
    onchange="this.form.changed=true">
    </form> 
    </body> 
    </html>

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's an interesting option, adios - I'll keep that one in mind! Although the way I showed above is shorter overall...

    (Now watch him top that... I know he will... )
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

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