Pouki Pouki - 1 month ago 6
ASP.NET (C#) Question

ASP.NET DefaultButton is focused on page load

I'm setting the propriety

DefaultButton
on my asp form, in order to send the form on the key
Enter
.

Everything is working fine, but my button has the focus state on the page load which is bothering because of some CSS styles applied on the page.

<form id="loginForm" runat="server" DefaultButton="btnLogin">
...
</form>


I would like the button to not be focused on the page load.

Thank you for your help

Answer

This is the default behavior of default button in ASP.NET. What you can do to override that is to set default focus manually on some other control. To do so, call Page.SetFocus:

Page.SetFocus(SomeOtherControlID);

This will set focus control on the form like that:

<form id="loginForm" runat="server"
      DefaultFocus="SomeOtherControlID"
      DefaultButton="btnLogin">