3

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)
  • Perhaps it has originated from an asynchronous method that happened after PreRender. – Jeremy Oct 05 '12 at 16:36
  • I have breakpoints on OnPreRender overrides at every level and not one of them fires before I get the error. Thanks for the suggestion tho. –  Oct 05 '12 at 16:38
  • I believe rendering does not get called during an asynchronous request. – Jeremy Oct 05 '12 at 16:39
  • If I remove the line of code that calles "SetFocus" then all works well and the OnPreRender events fire and hit my breakpoints. –  Oct 05 '12 at 16:41
  • Interesting, is the control hidden? – Jeremy Oct 05 '12 at 16:42
  • The control is a modalPopup panel in the process of being populated and shown. Inside the panel is a Textbox which the focus is being set on for usability. –  Oct 05 '12 at 16:46
  • Then I believe your best bet is to use JavaScript to set focus to the control instead of doing it server-side. – Jeremy Oct 05 '12 at 16:55

2 Answers2

2

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.

CanRobbo
  • 33
  • 7
0

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.

Shay
  • 1,680
  • 2
  • 26
  • 39