Why does __LASTFOCUS hidden field show up on only

2019-08-24 04:14发布

问题:

I'd been using the hookonfocus method from this article: http://www.codeproject.com/KB/aspnet/MainatinFocusASPNET.aspx

to maintain focus during postbacks for certain .aspx pages. However, I've noticed that several pages (and some very simple pages in a test project) don't include the hidden field __LASTFOCUS. I'm trying to figure out which options I'm missing that will cause this hidden field to show up and be used to restore focus if I use javascript to keep __LASTFOCUS up to date when focus changes happen in an ASPX page.

Note: I'm using VS 2005/.Net 2.0 in case that makes a difference.

回答1:

The __LASTFOCUS field is created by Page.RegisterFocusScript(), which is called from...

  • ClientScriptManager.GetPostBackEventReference(PostBackOptions, Boolean) if PostBackOptions.TrackFocus is set.
  • Page.SetFocus
  • BaseValidator.Validate() (via Page.SetValidatorInvalidControlFocus) to focus controls that fails validation

It is also called in the OnPreRender method of CheckBox, ListControl and TextBox (and any derivatives). I'm guessing that the pages that does not output the __LASTFOCUS field does not contain any variant of these three controls.