'SetFocus can only be called before and during PreRender

Can anyone tell me why I would be getting the error in the below trace?

"SetFocus can only be called before and during PreRender."

The error message says that "SetFocus" must be called before or during PreRender and based on the trace the page has come nowhere near anything to do with PreRender yet.

So why the error?

Copied From Trace.axd ...

Begin PreInit
End PreInit
Begin Init
End Init
Begin InitComplete
End InitComplete
Begin LoadState
End LoadState
Begin ProcessPostData
End ProcessPostData
Begin PreLoad
End PreLoad
Begin Load
End Load
Begin ProcessPostData Second Try
End ProcessPostData Second Try
Begin Raise ChangedEvents
End Raise ChangedEvents
Begin Raise PostBackEvent

SetFocus can only be called before and during PreRender.
  at System.Web.UI.Page.SetFocus(Control control)
  at System.Web.UI.Control.Focus()
  at ....ShowChangePasswordPopup(RecruiterClass recruiter, String commandName) in C:\...\RecruiterPopups.ascx.cs:line 1134
  at ....Default.Password_OnBeforeGo(MenuItem item, String queryString, Boolean forceRedirect) in C:\...\Default.Master.cs:line 92
  at ....MenuItem.GoDelegate.Invoke(MenuItem item, String queryString, Boolean forceRedirect)
  at ....MenuItem.Go(String queryString, Boolean forceRedirect) in C:\...\Menu\MenuItem.cs:line 129
  at ....MenuItem.Go() in C:\...\Menu\MenuItem.cs:line 115
  at ....MainMenu.lnkMyAccountProfilePassword_Click(Object sender, EventArgs e) in C:\...\UserControls\MainMenu.ascx.cs:line 130
  at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
  at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Solution 1:[1]

Calling SetFocus() on a page that is being posted back to and which results in a redirect to another page will cause this error. Try only calling Focus/SetFocus if !Page.IsPostBack and you'll find the error will no longer occur.

Solution 2:[2]

A few years late but to whomever runs into this...

We had a call to txt.Focus() in an btn.OnClick handler. It worked fine for many years. Recently we converted the handler to async and added a call to an API.

Something with that change caused the .Focus() call to throw this exception:

SetFocus can only be called before and during PreRender.

The solution was quite simple:

  1. Add a new handler for the button's PreRender event.
  2. Call txt.Focus() from this handler.

It might not be a viable solution for all scenarios, but it was for us.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CanRobbo
Solution 2 Shay