title: Set Focus to Any Control After PostBack
body: <p>First I have 3 TextBoxes, a DropDownList, a CheckBox, and 2 LinkButtons in a table. The TextBoxes and DropDownList must do an AutoPostBack because there are some calculations being done on the server side which called by OnTextChanged. </p>
  
  <p>My issue here is that after the content of any Control (which requires a PostBack) changes and you attempt to move focus to any other Control on the page (whether it be tabbing or using the mouse to click on any other Control) the focus does not move to the next Control or the Control you clicked.  </p>
  
  <p>I tried using a method which finds the current Control and based on the TabIndex, adds 1 which moves the focus to the next control. The problem here is that it does not allow for focus to be set on any other Control other than that one with the next TabIndex. To better explain here is an example of the situation.</p>
  
  <p>Example: I have TextBox1, TextBox2, and TextBox3. I change the content in TextBox2 then want to move backwards to TextBox1. When I click in TextBox1 the focus is set to TextBox3 since the code is using the order of the TabIndex. ** A key factor here is that TextBox1 DOES get focus before the server side code fires but then loses focus and sets it on TextBox3 **</p>
  
  <p>To me, it seems like the only way this can be accomplished would be to add a function on the client side which finds the Control with focus before the PostBack occurs, then reset the focus on that Control after the server side code completes. It does not seem like anything I do on the server side will work since the focus has already been lost when this occurs.</p>
  
  <p>I have searched and searched but have not been able to find any solution to this particular issue, everything I have found deals with setting focus to the NEXT Control instead of the Control you want to have focus. Any help would be greatly appreciated. Thank you in advance.</p>
  
  <p>Here is the code I am using to move to the next Control based on the TabIndex but this does not work in my situation.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          WebControl ctrl = GetPostBackControl() as WebControl;
          if (ctrl != null &amp;&amp; !SetNextFocus(Controls, ctrl.TabIndex + 1)) { ctrl.Focus(); }
      }
  
  public Control GetPostBackControl()
      {
          Control control = null;
  
          string ctrlname = Request.Params.Get("__EVENTTARGET");
          if (ctrlname != null &amp;&amp; ctrlname != string.Empty)
          {
              control = FindControl(ctrlname);
              control.Focus();
          }
          else
          {
              foreach (string ctl in Request.Form)
              {
                  Control c = FindControl(ctl);
                  if (c is Button)
                  {
                      control = c;
                      break;
                  }
              }
          }
          return control;
      }
  
      private bool SetNextFocus(ControlCollection controls, int tabIndex)
      {
          foreach (Control control in controls)
          {
              if (control.HasControls())
              {
                  bool found = SetNextFocus(control.Controls, tabIndex);
                  if (found) { return true; }
              }
  
              WebControl webControl = control as WebControl;
              if (webControl == null) { continue; }
              if (webControl.TabIndex != tabIndex) { continue; }
  
              webControl.Focus();
              return true;
          }
  
          return false;
      }
  </code></pre>