Optimize DataGrid ViewState

xiaoxiao2021-03-06  62

Come in the project increasingly using DataGrid, her sense of power, and easy to use, the self-contained paging is also able to take a small amount of data, but tested on the machine, upload to the server. .. Slow as a turtle .... View some page code, seeing ViewState accounts for 1MB.

Is it disabled? DataGrid's most features relying on ViewState, lazy people are reluctant to write these things with custom paging ...........

I have thought of a long time ago, I have seen this, because I can't pay attention to it, I just think of it, Google finds a lot of articles - it seems that I have been troubled by this problem. .....

The following appears:

JGTM2000BLOG a reply .......)

This article is very well understood, how to store DataGrid in useless data in ViewState (sometimes it is true) card, everyone knows that DataGrid will store all the data in the table in ViewState, this will Leading ViewState is very large; as compared with this forum, the viewstate takes more than 65K, more waste.

If you just simply set the DataGrid's EnableViewState to false, then many events, such as sorting, change, etc., will not be triggered, then don't you get lost? In fact, in DataGrid, there is a child control for rendering data: system.web.ui.WebControl.dataGridtable, unfortunate, I didn't find its instructions in MSDN, but when I tracked DataGrid, I found it A child control is the above Class (of course, this is what I know later, 嘿嘿). Everyone can take a look at the type of objects in SaveViewState, plus all values ​​in DataGrid.ViewState, will not find data saved in ViewState, then what causes viewstate, what is Data from the data source in ViewState?

The answer is in DataGrid's sub-control (this darkman's existence is really can't stand the feelings). So, if you don't need to store data from these data sources in ViewState (for example, in order to speed up the speed, or custom paging), we only need: DataGrid.Controls [0] .enableViewState = false, is OK.

original:

Thomas Skovsende

Hi there!

The Scenario is this:

I have alot of datagrids with data in them. This makes my viewstate Rather

Large (Upto 1MB), SO i Have Tried to Find Ways To FIX THIS.

I First Tried Storing My ViewState In A Session Instead by Overriding

SavepagestateTopersistencemedium / loadPagestateTopersistencemedium. This

Works Fine - Except When People Start To Open More Than One

Window (ViewStates Starts To Overwrite Eachother, Which Will Result In Aninvalid Cast.

Then I got the idea to make a new datagrid, inherited from the original

DataGrid - and Then Override The LoadViewState / SaveViewState Methods. (AND

Fetch The Data from The DB on Every Request

But While Doing this, I Discovered That Those Methods Only Are Responsible

For Saving ViewState for themselves - NOT Their Children (The DataGridTable

In The DataGrid - this is done by an undocunted method in Control Which

Is Defined As:

Internal Object SaveViewStateRecursive ()

The Problem Here Is That IT IS Not Virtual - SO I Cant Override It and Stop

The Framework from Storing The ViewState of The Actual Data in The DataGrid!

You Might Argue That I shop Disable The Saving of ViewState and CODE

Everything Manually - Well - TBH, I Find It Silly That I Have to make alot

Of extra code (and Ny That Introduce Possible Bugs), When All It Would Take

To Solve My Problem is to make saveviewstaterecursive () Virtual SO i CAN

Override it. if That Happend, I Would Just Override It for My Custom

DataGrid - ViewState Wouldnt Be Saved for My Grid, And no Bugs Would Be

Introduces (Less Likely Anyway!)

I Might Be Missing Some Obvious Solution To My Problem, But if Not I Would

Love an Argument for SaveViewStateRecursive () Not to Be Virtual. I am Aware

That there is a slight Performance Overhead, But I Would Say That IS

SO Little That It Doesnt Matter Much!

Best regards,

Thomas Skovsende

Thomas Skovsende

* Sigh *

Sometimes I Just Hate Being A Developer - After Battling with this qu.

For a few Good Hours, The Obvious Solution Appears:

Protected Override Object SaveViewState ()

{

Controls [0] .NableViewState = false; // disable viewstate for theDataGridtable - Hardcoded - But Works for Now!

Return Base.saveViewState ();

}

Ahh Well .. another case of "stupid development" ...: o)

Best regards,

Thomas Skovsende

转载请注明原文地址:https://www.9cbs.com/read-88391.html

New Post(0)