views:

352

answers:

2

I'll try and be concise:

1) I have a dropdownlist with Autopostback set to TRUE
2) I have an UpdatePanel that contains a Label.
3) When the downdownlist selection is changed, I want to update the label.

Problem: Focus is lost on the dropdownlist, forcing the user to click on the dropdownlist to reset focus back to the control.

My "solution": In the DropDownList_SelectionChanged event, set focus back to the drop down list:

dropdownlist1.focus()

Problem:

While this works great in IE, Firefox and Chrome change the scroll position such that the control which was assigned focus is positioned at the bottom on the visible portion of the browser window. This is often a very disorientating side effect.

How can this be avoided so it works in FF as it does in IE?

+2  A: 

Try MaintainScrollPositionOnPostback in one of these 3 ways

  • Programmatically - Page.MaintainScrollPositionOnPostBack = true;
  • Page declaration - <%@ Page MaintainScrollPositionOnPostback="true" %>
  • In the web.config - <pages maintainScrollPositionOnPostBack="true" />

You may also need to add this javascript after the scriptmanager declaration:

<script type="text/javascript">

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_beginRequest(beginRequest);

function beginRequest()
{
    prm._scrollPosition = null;
}

</script> 
James Westgate
+1 nice, didn't know about this feature.
Chuck Conway
Boy, I got excited-until I tried it and it didn't work. I added it to the project root web.config. I'm using VS2010 RC
Velika
Lets try putting in the page declaration and adding some script after the script manager (I had this problem, it sucked, I eventually got it to work)
James Westgate
Nope, that didn't seem to help.
Velika
+1 Hey I learned something new !
The_AlienCoder
Remove the focus code from the server code.use setTimer in script about 100ms after page is loaded to reset focus. (jquery will help here)
James Westgate
You learned nothing. It doesn't work.
Velika
I guess it doesnt work with the update panel.
James Westgate
A: 

try this one

var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_beginRequest(beginRequest); function beginRequest() { prm._scrollPosition = window.top; }
usman