-->

Maintain Control focus across post backs using Pos

2019-05-10 23:52发布

问题:

Maintaining focus across post backs is an apparently difficult task. Searching Google, you will find a ton of people that desire the same thing, but all hook it up differently, and mostly, custom-ly. I would like to avoid a custom implementation, especially if there's a way it's supported by .NET. Only after some very deep searching, did I come across PostBackOptions.TrackFocus, mentioned quietly in another stack overflow post. According to MSDN:

Gets or sets a value indicating whether the postback event should return the page to the current scroll position and return focus to the current control."

Holy crap, this is supported by .NET 4? AWESOME. But we have a ton of custom controls, how does .NET know how to set the focus on a control? I have no idea. Looking a the MSDN documentation for System.Web.UI.Control, there's an interesting method:

public virtual void Focus()

"Use the Focus method to set the initial focus of the Web page to the control. The page will be opened in the browser with the control selected."

Alright, clearly overridable. But what is the recommended method of doing so? It returns void. No examples. Unable to find any examples of people overriding this method in their implementations. However, after overriding it and doing nothing more than throwing an exception, it becomes evident that this is not how ASP.NET gets focus on a control that had focus before the post back: it never gets called.

After a ton of debugging using Firebug, I have found that enabling PostBackOptions.TrackFocus works! Sometimes. It is apparent that the focus of a control is only maintained when the control calls the __doPostBack JavaScript method. Other controls that launch a PostBack (when pressing enter inside the control), call WebForm_OnSubmit(), which doesn't update the ASP hidden field __LASTFOCUS. __doPostBack calls WebForm_OnSubmit() after setting the hidden fields.

This is where I'm currently stuck. It's looks as if I need to get everything to call __doPostBack, no matter what. There's very, very little documentation on the use of TrackFocus. So does anyone have any tips from here?

回答1:

I've been maintaining focus accross postbacks using the method in this article: (ie: store focus in __LASTFOCUS hidden field on field enter event clientside for all controls)

http://www.codeproject.com/KB/aspnet/MainatinFocusASPNET.aspx

If you've gotten as far as having __LASTFOCUS show up on the page, this should get you most of the rest of the way...

Note: It'd be nice to find a way to keep the extra javascript from bloating __VIEWSTATE for example.

It was working pretty well for me until I figured out that some of my pages included the hidden __LASTFOCUS field and some of my pages didn't. (That's what prompted me to search around and find your question) Now I'm just trying to figure out how to make sure __LASTFOCUS always shows up on every page I want to keep track of focus on... (Looks like I'll have to open a separate question about it)



回答2:

Here is what I just did. Assuming you have a handler in your code behind that takes care of the event and has a signature like this:

protected void myEventHandler(object sender, EventArgs e)

You can use this line of code to restore focus back to the sending object:

        ScriptManager.RegisterStartupScript((WebControl) sender, sender.GetType(), "RestoreFocusMethod", "document.getElementById(\"" + ((WebControl) sender).ClientID + "\").focus();", true);

just using the Focus() method of the sending control will reposition the page (if you are scrolled down a bit), but this works beautifully. And if you have specific handlers for your control, you can just use the control itself rather than casting the sender to a WebControl, like this:

    protected void CityListDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        ...
        ScriptManager.RegisterStartupScript(CityListDropDown, CityListDropDown.GetType(), "CityDropDownRefocus", "document.getElementById(\"" + CityListDropDown.ClientID + "\").focus();", true);
    }