Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView row positioning after postback

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
US
I've been struggling with this for 2 days now. How the heck do you handle scroll repositioning of a <div> after a postback of an ASP.NET web page?

I have a gridview inside a div. Scrolling through the gridview is actually being handled by scrolling through the div. Gridview controls don't have a scrollbar property.
Let's say that I scroll down to the 100th record of the gridview and click a button in there. I successfully handle the SelectedIndexChanged event and retrieve data from the gridview at that row.
Once the postback concludes, however, the position where I was in the div is lost and it positions itself at the top. That behavior is very annoying to the users because they have to manually scroll to the row they were at. Fortunately, I change the color scheme on the selected row so that can at least find where they were.

I've found dozens of developer forums where folks have posed this same question. Solutions range from java script to jquery. I've tried them and nothing works. All of the javascript solutions make reference to a scrollTop property for the div. That property doesn't even exist! I don't have access to jquery so wouldn't have any idea how to implement that.

My web page is embedded in a master page if this helps.

Does anyone have a solution for this problem?

Thanks in advance,
Jerry

Jerry Scannell
 
All of the javascript solutions make reference to a scrollTop property for the div. That property doesn't even exist!
There is a property scrollTop which does not do any scrolling, you have to use animate to scroll the object.

I don't have access to jquery so wouldn't have any idea how to implement that.
Everyone has access to JQuery. You just need to include a link to the CDN files or download the files and include them in your project. Using jquery you can use <element>.animate()

You would have to trap the position of the div when the row is clicked and scroll to that position after postback. I am not saying it's going to be easy, but it can be done.
Why do you have a gridview in a scrollable div anyway? If you have a lot of rows, then use paging. This way on postback, the same page will be shown and the row highlighted and in view.
 
Isn't there a MaintainScrollPositionOnPostback="true" attribute you can just add to the page directive at the top of the master page to handle that?
 
There is but it only works in IE. Also, it will only scroll the page. He has the gridview inside of a scrollable div so the div needs to be scrolled not the page.
this is why it gets a bit more tricky to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top