Scrolling with a Datagrid in a DIV

Today I got the chance to make use of Jim Ross's excellent article, “Maintain Scroll Position in Any Page Element”.  It describes when using a scrollable Datagrid--one inside an overflow-auto DIV control--how to maintain the user's scroll position inside the DIV across postbacks, such as when a Select button is clicked.  It does this using IE behaviors, HTC files, which I hadn't used before, so that was fun, and it also solved my problem.

I also liked the ReadMe.txt that came with the sample downloaded code:  “The sample application is written in VB. If you only know how to read C#, stop whining and learn to read another language. It will be good for your soul, and your career.” :)

P.S. Jim also has a new article out about Datagrids and ViewState, check it out.

Published Thursday, September 04, 2003 2:23 PM by datagridgirl

Comments

# re: Scrolling with a Datagrid in a DIV

"stop whining and learn to read another language. It will be good for your soul, and your career.”

Could go both ways.

Thursday, September 04, 2003 5:43 PM by Anon

# re: Scrolling with a Datagrid in a DIV

Could go both ways for many people on dotnetweblogs

Thursday, September 04, 2003 5:44 PM by Anon2

# re: Scrolling with a Datagrid in a DIV

That's the whole point--BOTH C# and VB people should be able to read the other language, it just makes life so much simpler.

Thursday, September 04, 2003 5:54 PM by Datagrid Girl

# re: Scrolling with a Datagrid in a DIV

IMO, using a behavior for this is pretty lame, as is just hard coding it in your HTML every time and it isn't going to work outside of IE. Here is an improvement:

1. Create a custom web control class ScrollablePanel
2. override OnPreRender to register the onScroll script / onLoad script via RegisterClientScriptBlock
3. override CreateChildControls method and add your hidden field.
4. override your control's render method to output your custom DIV tag.
5. add ScrollPosition property
6. implement IPostBackDataHandler / LoadPostData.

Now you have a .NET control that you can reuse to your heart's content.

Thursday, September 04, 2003 6:02 PM by Jesse Ezell

# re: Scrolling with a Datagrid in a DIV

> That's the whole point--BOTH C# and VB people should be able to read the other language, it just makes life so much simpler.

Well sort of. The whole point is that 90% of the syntax in *either* language is BCL or CLR stuff so the CLR language is irrelevant. I have been saying this for over 2 years now and people just don't want to get it. If you can read VB.NET you can read C# and vise versa because most of the code is BCL calls (like System.Console.WriteLine for instance). Its WriteLine no matter what the CLR language is. When people get to "Its the Runtime Stupid" and finally accept that, then they will get somewhere.

Thursday, September 04, 2003 6:43 PM by Sam Gentile

# re: Scrolling with a Datagrid in a DIV

Hi Jesse, in my case using an IE behavior wasn't "lame", as it's for an IE-only application.

Thursday, September 04, 2003 7:13 PM by Datagrid Girl

# re: Scrolling with a Datagrid in a DIV

This just shows a difference in philosophies I guess. Generally, I spend my time building libraries, so I am always in code reuse mode. Here at the office, we have at least one assembly that is referenced in the majority of the projects we are working on. It contains utility classes such as this as well as other common web controls (such as listview and treeview) that aren't part of the .NET framework (another assembly is also reused in 90% of our projects which contains our custom authentication / profile / security classes). Although the first time around, the benefits aren't immediately realized, it saves quite a bit of time in the long run.

Anyway, you are still better off not using a behavior, even with an IE only application. First off, it takes about the same amount of work to do it either way (maybe 5 extra minutes of coding to create the control version). Secondly, using a behavior makes reuse of your scrollable div a pain (you can't package in a reusable dll). Third, if you have more than one scrollable grid in your application, you are going to be duplicating a lot of code (not to mention that you can use a scrollable panel for any scrollable content area, not just a grid). Fourth, even if THIS application was an IE only application, there is no gaurentee that your next project will be. And finally, as this functionality is pretty generic, not doing things right the first time around will only cause you extra work the second time around.

I guess everyone has a different oppinion on how important reuse is though... Some people like getting paid to write the same code over and over again :-). In any case, as I noted previously, this is all IMO. Everyone is free to disagree.

Thursday, September 04, 2003 8:07 PM by Jesse Ezell

# re: Scrolling with a Datagrid in a DIV

It's good to see that Marcie's taste in articles is as good as her taste in husbands. [grin].

I readily acknowledge the criticism against HTC's, but I also will point out that I specifically stated in the article that the htc could be a limitation.

Re: a "lame" approach and I should have created a custom web control, I fully agree that this kind of thing is an excellent candidate for a custom control. However, my purpose wasn't to write a treatise on web controls, but to show how to solve the scrolling problem. Now that the technique has been shown, I freely grant permission to anyone who wishes to incorporate it into a custom control and write an article that shows how to do it. But for myself, I have found that StrengthTechnologies excellent "SmartScroller" has already done that.

Sunday, September 07, 2003 1:52 AM by Jim Ross

# re: Scrolling with a Datagrid in a DIV

Making it a custom control is not as easy as you might think. It's actually pretty darn complicated. At least it is if you want to break the header out of the table.

Friday, December 05, 2003 4:56 AM by Robert McLaws

# re: Scrolling with a Datagrid in a DIV

I'd like to see an article related to this subject - how to sort a dataGrid when headers exist outside of the dataGrid (with and without making a round trip back to the database).

Keep up the good work Marcie

Tuesday, March 02, 2004 2:07 PM by Kathy Bungard

# how to change the size of scrollbar relative to table size

hi,
my problem is that size of the scroll bar is not increasing or decreasing with the increase in size of the table.kindly resolve this problem.

Tuesday, March 09, 2004 5:19 AM by rakesh kumar

# Scroll part of page


My Page Has Common Top Header
Under the Header dynamic message occurs
Whenever scroll the page
the top remains visible up to end of scroll
How?

Wednesday, July 28, 2004 8:25 AM by chandrakumar

Leave a Comment

(required) 
(required) 
(optional)
(required)