title: Set focus to text box from code behind after validation the inputs in asp.net web application
body: <p>What I want to do is to validate the given input by the user and then set a focus on the invalid input in text box.</p>
  
  <p>My save button function is given below.</p>
  
  <pre><code>protected void btnSave_OnClick(object sender, EventArgs e)
  {
      try
      {          
          if (ValidateForm())
          {
              Users objUser = new Users();
              objUser.Username = this.txtUsername.Text;
              objUser.Password = this.txtPassword.Text;
              objUser.Add();
  
              this.labelMessage.Text = "User has been saved successfully";
          }
      }
      catch (Exception ex)
      {
          Monitoring.WriteException(ex);
      }
  }
  </code></pre>
  
  <p>Validation function to validate the given input is not null or empty.</p>
  
  <pre><code>private bool ValidateForm()
  {
      bool isvalidate = true;
  
      try
      {
          string username = this.txtUsername.Text;
          string password = this.txtPassword.Text;
  
          if (username == "" || username == string.Empty)
          {
              this.labelMessage.Text = "Please enter username";
              this.txtUsername.Focus();
              isvalidate = false;
          }
          else if (password == "" || password == string.Empty)
          {
              this.labelMessage.Text = "Please enter password";
              this.txtPassword.Focus();
              isvalidate = false;
          }
      }
      catch (Exception ex)
      {
          Monitoring.WriteException(ex);
      }
      return isvalidate;
  }
  </code></pre>
  
  <p>The problem is the I am unable to set focus to any textbox after validation.
  Anyone know where I am wrong? 
  Or there is any other better way?
  I have searched a lot but most of them setting the focus on Page load (Post back). I want to do it from code behind.</p>