Saturday, March 24, 2012

Waving the white flag..scroll position.

Hey guys,

I'm loosing my mind over here trying to figure this out. I'm hoping someone can help me out.

Problem:

I have a datalist with a pager in an update panel which is all in a content page. The scriptmanager is in the master page. When i click on the different page numbers the datalist changes page but the scroll position stays at the bottom of the page where the pager is. I'd like to reset that scroll position to the top of the update panel but im having no luck. I've googled and searched this forum for a good 4-5hrs with no luck. I've found plenty of potential solutions but nothing working. Even setting focus to a hidden control isnt working. My only constraint is that I only want to reset teh scroll position when I move pages. not when there are other types of postbacks.

I've tried this code (im placing it in my content page) but it doesnt do anything ... im not sure if i need something in my codebehind aswell...:

<scripttype="text/javascript">

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

prm.add_endRequest(EndRequestHandler);

function EndRequestHandler()

{

prm._scrollPosition =null;

}

</script>

Thanks,

http://aspnet.4guysfromrolla.com/articles/111704-1.aspx

<pages smartNavigation="true"MaintainScrollPositionOnPostback="true"/>

Usually People loves to maintain the scroll postiion. But requirment is reverse, Here is the trick, create an script tag after the scriptmanager and put the following lines:

<script type="text/javascript">var prm = Sys.WebForms.PageRequestManager.getInstance();prm.add_beginRequest(beginRequest);function beginRequest(){ prm._scrollPosition = null;}</script> 

Let me know if this helpful to you


hi chetan,

Thanks for the reply but i've tried that code aswell and still nothing happens. My script manager is on my master page so i dont want every page to postback to the top of the page only certain pages. I've tried placing that code in my <content> page but nothing happens. Is there anything else i need to do?

Does anyone else have any suggestions? I've tried enabling and disabling MaintainScrollPositionOnPostBack but it doesnt seem to have any effect.

Thanks


In all the posts i checked the same code above was given but thatdidn't work for me at all. However i was able to solve this problem.

I used the EndRequestHandler instead of BeginRequest

I used window.ScrollTo(0,0) instead of the prm._scrollPosition = null <-- this was the main problem

In order to only scroll to 0,0 when my pager was clicked:

I used the IntializeRequest handler and i stored the args.get_postBackElement() in a var.

Then back in my EndRequestHandler i checked to see if that was equal my datalist's clientId.

To make this more flexible i wraped it in a usercontrol with a PBControlId property that i set within the pages i use the control on.

Hope this saves ppl time.

No comments:

Post a Comment