-
-
Fritz raises an interesting point about grids and viewstate. I replied suggesting that simply resetting the DataSourceID property (to the same value) triggers a process that in the end fixes the issue. Sure, it's kind of hacky, but it works.
However, I coundn't agree more with Fritz that 1) disabled viewstate on the grid; 2) caching through the data source control are overall the best approach to displaying data through grids without killing the page--in download, but also in upload.
I thought I should share a few additional remarks on the topic here.
Caching is fine, especially now that it is kind of "free". You just set EnableCaching and go. Note that, for some reasons, caching is not enabled (Beta 2, at least) on ObjectDataSource when you use custom collections. In this case, caching should be implemented in the bound objects--your BLL.
- Easy and blissful caching might lead to sub-optimal code if this means that you let the page cache everything results from a query. Keep the size of the data in the cache under control.
- Back to viewstate and grids, note that the size of the viewstate that results from a grid (both DataGrid and GridView) mostly depends on styles and additional helper properties being saved. Control state is not the point here, as it is saved to the viewstate hidden field anyway. It differs from classic viewstate because of the different API and because it can't be disabled.
- True data in the viewstate for a grid is not really much--just the displayed strings.
While disabling the viewstate is definitely a good option keeping everything else that is critical to the control to the control state, I'd spend some thoughts on the fact that style properties take up most of the viewstate and they're 90% of the time constant and written to the .aspx when not in a theme. Why persisting to the viewstate? With this info in the viewstate, the grid can rebuild itself with no data access when in the IsPostBack branch. This makes sense if you want to save a roundtrip to the database, but not if you have data cached.
-
-
Although they still use it as if nothing was the matter, some people are a bit worried about viewstate and security. My personal opinion is that you should be concerned about the view state, but not for the potential security holes it might open in your code—unless you love shooting yourself in the foot. You should be more concerned about the overall performance and responsiveness of the page. Especially for feature-rich pages that make use of plenty of controls, the view state can reach a considerable size, measured in KB of data. Such an extra burden taxes all requests, in download and upload, and ends up being a serious overhead for the application as a whole. Let's review some numbers trying to learn from best practices.
You should endeavour to keep a page size around 30 KB, to the extent that is possible of course. For example, the Google’s home page is less than 4 KB. The home page of ASP.NET counts about 50 KB. The Google’s site is not written with ASP.NET so nothing can be said about the viewstate; but what about the viewstate size of the home of the ASP.NET site? Interestingly enough, that page has only 1 KB of viewstate. On the other hand, this page on the same site (ASP.NET) is longer than 500 KB of which 120 KB is viewstate.
The ideal size for a viewstate is around 7 KB; it is optimal if you can keep it down to 3 KB or so. In any case, the viewstate, no matter its absolute size, should never exceed 30% of the page size.
PS1: In ASP.NET 2.0, a new serialization format (incorporated in a new formatter class, ObjectStateFormatter), significantly cuts down the size of the viewstate by about 50%. Which is just great! All that you have to do is recompile...
PS2: All numbers mentioned are based on the size of the pages as of July 20, 2005.
-
-
Another relatively common scenario for Web apps. You display a list of items to the user, the user can click to perform any action on the displayed items but also might want to request more information. To exemplify, imagine a shopping application with a grid of products and a Add to Cart button. Next, you need your users to get a preview of the product and more information that for space constraints couldn't show up in the grid row. How would you do that?
- Option #1: You add a link somewhere (say, More on this item) and open a popup window. If you hate popups, you can simply redirect the user to another new page. It works, but you risk to flood the user with too many (and perhaps undesired) browser windows.
- Option #2: You add a hyperlink column and an IFRAME tag into the page. You direct the output of the linked page to the target represented by the IFRAME.
To me, the latter option has a few advantages. First, you use distinct pages for the grid and information. Second, users see what they need without refreshing the whole page or opening a brand new one putting at risk the already limited screen real estate.
Using IFRAMEs (inline frames) is a debatable choice; but all considered if you like the result you actually get it's probably a good compromise between usability and portability. Read more here. I try to use IFRAME whenever possible. Some sparse considerations:
- IFRAME is no longer an IE-only feature and is part of the HTML 4.0 standard
- FireFox supports IFRAME just fine. I didn't try with Netscape 7.x, but someone told me it supports it too.
- If you incorporate IFRAME in your Web page, pay attention to close it explicitly with </iframe>. For some reasons, it cuts the remainder of the page if you don't close the tag or use the shortcut.
- Sure, IFRAMEs have a number of additional problems (i.e., printing, unreachable URL, dimensions) you need to deal with. Latest browsers seem to be able to stem the growth of security risks.
I also like the _search target, that is the IE and FireFox ability to open a page in the search panel, a window docked on the left edge of the browser's window. Functionally speaking, it's a good way to avoid IFRAMEs while serving users with new pages in the same screen.
NB: If you google for "_search target" you easily run into articles that report security bugs connected to the feature, especially for Mozilla FireFox. One that I've found dates back to mid April 2005. As I understand it, those security bugs relate to the browser rather than to using _search panes from within an ASP.NET page. Security risks are related to the fact that some malicious script can exploit the _search pane to follow links. Displaying a page that puts a read-only, nonclickable piece of HTML in the pane doesn't seem to be a security hazard to me. However, having users to apply the latest patch of their browser of choice is anyway a good thing.