Skip to main content

Set focus to a control from code behind, ASP.NET!

I tried to focus on error message in a control (asp.net table) but I couldn't get it. So I added a hyperlink control next to the table and set focus on it. It worked perfectly.

What I did:

  • Add the following code in aspx.
     <asp:HyperLink ID="LnkFocus" runat="server">
     </asp:HyperLink>
  • Add this in code behind
     LnkFocus.Focus();

That's all. I think this is one of the easiest way to set focus on a control.

Comments