Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem tabbing field focus with SmartNavigation and AutoPostBack

Status
Not open for further replies.

johnaregan

Programmer
Mar 13, 2001
87
0
0
This relates to a problem I came across when using the tab key to move the focus around a page of text boxes. All textboxes have AutoPostBack set to true and the page also has SmartNavigation switched on.

When you change a value in a text box and tab out, the page is posted back as expected. A user would expect that the focus would change to the next field in the tab order as it does when SmartNavigation is not switched on, but it does not. Instead the focus returns to the changed text box, even if you put the usual asp code in the body tag:
Code:
  document.getElementById('NextField').focus();

Looking through the SmartNav.js script is this line:
Code:
  window.setTimeout(sn.restoreFocus,0);
The restoreFocus function puts the cursor back to the original field and the javascript function setTimeout makes sure it is executed last as the page loads. This explains why the body tag code does not work and also why you can see the cursor flicking briefly to the correct field.

Client Side
To workaround this just use this javascript in the <head> tag. The delay of 1 millisecond makes sure that FocusHere is fired after the restoreFocus function above has run which has it's delay set to 0 milliseconds:
Code:
<script language=javascript id="noHereScript">
  window.setTimeout('FocusHere();', 1);
  function FocusHere()
  {
    var el = document.getElementById(<%=GotoThisTextBox()%>);
    el.focus();
  }		
</script>
Server Side
You can write a method on the server side called GotoThisTextBox() which returns the ID of the text box that you want to set the focus of and so sets the value in the client side script above. elPost is a class member variable which can be set anywhere in the code:
Code:
public class WebForm1 : System.Web.UI.Page
  {
  protected string elPost;
  ....

protected string GotoThisTextBox()
{
  return "'" + elPost + "'";
}

At the moment, I am setting the value of it in the TextChanged event of the TextBox control.
So, for example, if TextBox1 has changed value, then the TextBox1_TextChanged method will fire on the server side and in the method I have set the value of elPost to TextBox2 (there is probably a better way of setting the value of elPost based on a page tab order).
 
I had the same issue. Here is what I eneded up with thread855-972405
I have cleaned up the javascript a bit since I posted it and use __doPostBack. Not sure how your solution would handle clicking on a box out of sequence order.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top