...

View Full Version : Finding control focus



Vermelh0
09-26-2002, 08:45 PM
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

mordred
09-27-2002, 01:03 AM
Control? Postback? Care to elaborate?

Vermelh0
09-27-2002, 09:06 PM
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

Roy Sinclair
09-27-2002, 10:47 PM
You could do something like this:



<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.

whammy
09-28-2002, 02:39 AM
http://www.codetoad.com/asp.net/aspnetcontrols10.asp

Vermelh0
09-28-2002, 10:04 PM
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

whammy
09-29-2002, 01:58 AM
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!

:)

adios
09-29-2002, 05:28 AM
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.

Vermelh0
09-29-2002, 08:20 PM
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

whammy
09-29-2002, 08:35 PM
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...



<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:



<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.

:confused:

adios
09-29-2002, 11:10 PM
Not sure I get what you're up to, either, but, it's your thread:


<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>

whammy
09-30-2002, 03:19 AM
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... ;))



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum