Vishwakant Tripathi

Vishwakant Tripathi

  • NA
  • 717
  • 819.8k

How to set focus on a control in asp.net?

Aug 23 2016 9:33 AM
Hi,
     I have one asp.net page, which has a Button (Submit button) that has PostBackUrl ="#" that helps to maintain scroll position during page post back.
I have set MaintainScrollPositionOnPostback="true" in Page directive on the page.
 
Submit button code behind is something like this ...
 
protected void SubmitButton_Click(object sender, EventArgs e)
{
      try
      {
         txtName.Focus();
      }
      catch (Exception ex)
      {
         //error
      }
}
 
Where txtName is a textbox on the page which I need to focus. (Not exactly focus to textbox only but some other logic that I have removed from here.) 
 
So can someone please help me to get rid of this issue.
 
Thanks.
Vishwakant. 
 

Answers (8)