保持整个使用PostBackOptions.TrackFocus后背上控制重点 [英] Maintain Control focus across post backs using PostBackOptions.TrackFocus

查看:183
本文介绍了保持整个使用PostBackOptions.TrackFocus后背上控制重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在整个后背上维护的重点是一个明显困难的任务。谷歌搜索,你会发现一吨的这一愿望同样的事情的人,但都勾起来不同,且多为定制-LY。我想避免一个自定义的实施,特别是如果有它是由.NET支持的一种方式。只有在一些非常深的搜索,我才遇到PostBackOptions.TrackFocus,在另一个堆栈溢出后悄悄地提及。根据MSDN:




获取或设置指示回发事件是否应在页面返回到当前的滚动位置和焦点返回到值电流控制。




哇靠,这是由.NET 4?支持真棒,但我们有一吨的自定义控制,如何?.NET不知道如何设置集中了控制,我不知道找一个为System.Web.UI.Control MSDN文档,有一个有趣的方法:

 公共虚拟无效对焦()




使用对焦方法来对网页的初始焦点设置到
控制。该页面将与选择的控制
中的浏览器打开。




好吧,显然重写。但是,什么是这样做的推荐的方法是什么?它返回void,没有例子。无法找到的人在他们的实现重写此方法。然而,覆盖它,无所事事不是抛出异常的详细经过任何的例子,它变得很明显,这不是多么ASP .NET得到重点介绍了职前集中回控制:它永远不会被调用。



一吨使用Firebug调试后,我发现,使PostBackOptions。 TrackFocus作品!有时候,这是显而易见的,当控件调用__doPostBack的JavaScript方法控制的重点仅仅维持。这次发射回发(按下时输入控件中)其他控件,调用WebForm_OnSubmit(),这不更新ASP隐藏字段__LASTFOCUS。__doPostBack设置隐藏字段后调用WebForm_OnSubmit()。



这就是我目前卡住。这看起来好像我需要的一切调用__doPostBack,不管是什么。有非常,对使用TrackFocus很少的文档。 ?因此,没有人在这里有任何提示


解决方案

我一直保持专注翻过使用该方法在这篇文章中的回传:
(即:存放焦点__LASTFOCUS隐藏字段的字段中输入事件客户方对所有控件)



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



如果你已经尽可能得到具有__LASTFOCUS显示在页面上,这应该让你最的方式休息的...



请注意:这d是很好的找到一种方法来保持从腹胀如__VIEWSTATE额外的JavaScript。



这是工作很适合我,直到我想通了,我的一些网页包含隐藏__LASTFOCUS领域和一些我的网页没有。 (这就是促使我四处搜寻,找到你的问题)现在,我只是想弄清楚如何确保__LASTFOCUS总是显示了每一页我想保持正轨精力放在...(貌似我会要开一个关于它的另一个问题)


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?

解决方案

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)

这篇关于保持整个使用PostBackOptions.TrackFocus后背上控制重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆