|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article shows you how to reduce size of ViewState data generated by I assume that you know what it's and how to use ViewState. If not, please first learn about them, e.g. read article Taking a Bite Out of ASP.NET ViewState. In that article you also find general information about reducing size of ViewState. BackgroundWhen I started to learn and use ASP.NET in practice, one of my problems was ViewState - usually it's too large! Usually I can reduce its size, by disabling it for selected controls or for whole page. Unfortunately, when I disable VS for control, sometime I lost some of its functionality. This is true for In last two weeks I decided to finally solve this problem. I assumed that First I displayed the whole tree of controls used to display my page. First surprise -
Next I decompiled .NET binaries using Reflector [^] and I browsed code of In this point, I decided to try to decode and analyze contents of my ViewState. I used ViewState Decoder [^] for this. When I displayed my ViewState's data, its structure looked familiar for me. After wards, I found that - it's similar to control tree on my page! And I think - what if I disable ViewState for each row of How to reduce size of DataGrid's ViewState - summaryDisable ViewStateIf you can, disable the ViewState for whole Disable columns autogenerationSet property Disable ViewState for each DataGrid's rowThis is the best method, because copy of displayed data is stored in ViewState as values of private void DisableViewState(DataGrid dg)
{
foreach (DataGridItem dgi in dg.Items)
{
dgi.EnableViewState = false;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
MyDataGrid.DataSource = GetData();
MyDataGrid.DataBind();
DisableViewState(MyDataGrid);
}
Store required identifiers in ViewState as string[] arrayWhen you display data for user, you need to store identifiers of displayed data. You can store them in hidden Example: You have array of three ids: 1, 2, 3. If you store them in ViewState as @<1;2;3;>
When you store them as @System.Int32, mscorlib, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089<i<1>;i<2>;i<3>;>
If you store more @50<i<1>;i<2>;i<3>;>
Alternatively you can store your ids in l<1;2;3;>
l<i<1>;i<2>;i<3>;>
Here is an example code that creates and stores array with ids in ViewState: DataTable dt = GetData();
MyDataGrid.DataSource = dt;
MyDataGrid.DataBind();
string[] ids = new string[dt.Rows.Count];
for (int n=0; n<dt.Rows.Count; ++n)
ids[n] = dt.Rows[n]["id"].ToString();
ViewState["ids"] = ids;
When you need to retrieve id of displayed data, use following code: int index = MyDataGrid.SelectedIndex;
string[] ids = (string[])ViewState["ids"];
int myID = Convert.ToInt32(ids[index]);
StatisticsI created page that display a 10*10 table. Each cell contains a three digit number. Below are results of my tests: VS - ViewState, CA - columns autogeneration.
As you see ViewState, size is reduced from 6032 to 316 bytes. We saved 5716 bytes, and now ViewState contains only 5.2% of the beginning data! Points of InterestI worked on primary method covered in this article (disable ViewState for each DataGrid's row) for almost two weeks. It's true that simplest solutions are most difficult to find. I found also that, data stored in ViewState could be optimized. This can be done if you know how History
|
||||||||||||||||||||||||||||||||||||||||