ViewState enabled=false no affect?
I have a Gridview that has some controls above you can use to select how much data the Gridview displays - a week's worth, a month's worth, a year's worth etc.
And, as it's a view only grid, I thought 'I'll keep the page size down by doing this:
<asp:GridView EnableViewState="false" etc.
But, if I look at a year's worth of data and look at Page Source - the ViewState is massive. If I look at a week's work, the ViewState is small. A month's worth - a bit bigger and so on.
So, despite setting EnableViewState to false - the ViewState does seem to grow in proportion to the data being displayed in the Grid. I don't need all that data held in ViewState so why is it happening?
Thanks for any help.
Re: ViewState enabled=false no affect?
Have you checked the size difference of the viewstate between when EnableViewState="true" and EnableViewState="false"? Like how big is the viewstate of gridview (showing a week's worth) when EnableViewState="true" and EnableViewState="false". I think you should see that the viewstate size is less when it is set to false.
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
benmartin101
Have you checked the size difference of the viewstate between when EnableViewState="true" and EnableViewState="false"? Like how big is the viewstate of gridview (showing a week's worth) when EnableViewState="true" and EnableViewState="false". I think you should see that the viewstate size is less when it is set to false.
I have checked the size difference. I've since read that for data bound controls like the GridView ViewState contains control data too - so if you have a lot of DataKeyNames (I have 13 in this Gridview) they all get encrypted and stuck in ViewState. So, I guess I'm stuck with it.
Thanks for your reply.
Re: ViewState enabled=false no affect?
I'm not sure that the gridview is the problem here unless you manage to stuck a years worth of data in one page.
If you have a gridview with 100000 records and you show 10 on the page the viewstate will have only this 10 records info, maybe you are doing something wrong?
If you only display few records at a time and you still get humongous viewstate then can you provide a snapshot of the viewstate you get and the gridview records you have?
Re: ViewState enabled=false no affect?
A couple of questions to sort out what is happening.
1. What are you binding the grid to... sqldatasource/objectdatasource/datareader
2. is there paging or sorting = true on the grid
3. are there any controls in the grid with viewstate=true
With the grid viewstate = false it should not contribute to page viewstate at all so I'm guessing something associated with it is.
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
sapator
I'm not sure that the gridview is the problem here unless you manage to stuck a years worth of data in one page.
If you have a gridview with 100000 records and you show 10 on the page the viewstate will have only this 10 records info, maybe you are doing something wrong?
If you only display few records at a time and you still get humongous viewstate then can you provide a snapshot of the viewstate you get and the gridview records you have?
Well, as it happens, it is a calendar/timeline based application and yes, the maximum I allow users to view is a year's worth of data. In most situations the maximum number of rows is 1000 or so. I don't want to put paging on the grid as paging is clunky - the diary records events in project timelines and people need to be able to look at some event - a meeting date perhaps - and say 'when was the agenda circulated' and just scroll up to find when the agenda was circulated - not start clicking buttons to page.
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
brin351
A couple of questions to sort out what is happening.
1. What are you binding the grid to... sqldatasource/objectdatasource/datareader - datareader on one page, data object on another
2. is there paging or sorting = true on the grid - there is no paging or sorting
3. are there any controls in the grid with viewstate=true - yes, in that I haven't explictly set the controls in the gridivew to viewstateenabled="false' because the grid has been set to vewstateenabled="false' - I figured this would cover it - but maybe it doesn't?
With the grid viewstate = false it should not contribute to page viewstate at all so I'm guessing something associated with it is.
So, is what I've read about gridviews maintaining the DataKeyNames collection in controlstate - which is pushed into viewstate - wrong?
Re: ViewState enabled=false no affect?
I'm strongly against displaying 1000 rows of data in one page.You will get what problems you are getting now.
I would have used paging by all means.Of course not the standard gridview paging that sucks bad but something custom.Maybe a paging control that manipulate gridview paging with sql sp's on the backbone.
Re: ViewState enabled=false no affect?
Quote:
So, is what I've read about gridviews maintaining the DataKeyNames collection in controlstate - which is pushed into viewstate - wrong?
No - I tested it in asp.net 2 and 4 and grid datakeys add to page viewstate regardless of viewstate=false.
I guess this could be considered a flaw but why would you have datakeys if viewstate=false anyway?
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
brin351
No - I tested it in asp.net 2 and 4 and grid datakeys add to page viewstate regardless of viewstate=false.
I guess this could be considered a flaw but why would you have datakeys if viewstate=false anyway?
Having populated the DataKeyNames collection when binding the grid - in the RowDataBound event I access the DataKeyNames collection to do things like ... if the value of key[7] is the same as key[7] in the row before, make the first and second cell span two rows ... if the value of key[9] is false, hide this linkbutton etc. etc.
I kind of assumed that this meant ... while processing on the server, do various things to this grid and then send the grid, as html, to the browser. I would have thought that saying viewstateenabled=false for the grid would mean ... 'I do not intend to try to retrieve data on postback for this grid, so the DataKeyNames collection can be discarded and there is no need to send it to the browser within viewstate.
Thanks for your reply.
Re: ViewState enabled=false no affect?
Ok that makes sence.
Perhaps in grid.unload event set grid.datakeynames = nothing. Thats a stab in the dark but it may work.
Re: ViewState enabled=false no affect?
Hello,
Bottom line, there is always going to be some "bloat" added to the page by using ASP.Net Server controls. If you are worried about this to the point that you are looking to optimise, then have you considered using alternative?
i.e. created the required HTML on the fly and inject this into the page?
Gary
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
gep13
Hello,
Bottom line, there is always going to be some "bloat" added to the page by using ASP.Net Server controls. If you are worried about this to the point that you are looking to optimise, then have you considered using alternative?
i.e. created the required HTML on the fly and inject this into the page?
Gary
It's funny you should suggest that - in fact I have two versions of the same page - one lays the data out horizontally - project team members are column headings and the project timeline forms the rows. In the other (the one I have a problem with) the layout is still calendar going down the page - but each project member appears to have a row within each day and their tasks are shown in the next column.
The horizontal layout is impossible to get back from the database as a set of data. Instead I populate 3 objects - one is the list of project members - one is the list of dates - and the 3rd is just a list of DateID|ProjectMemberID|TaskID|Task description etc.
I do construct the html on the fly and at the intersection of each column and row I can get at the DateID | Project MemberID and I use this to filter the 3rd dataset so I can put the tasks in the right place in the table. Showing a year's worth of data this way is significantly faster to get on the screen than what I thought would be quicker - i.e. just using one dataset to populate a Gridview with, admittedly, a fair bit of mucking around to get rowspans and columnspans right.
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
brin351
Ok that makes sence.
Perhaps in grid.unload event set grid.datakeynames = nothing. Thats a stab in the dark but it may work.
Well, well, well - I tried that.
Code:
protected void gvProjectTimeline_DataBound(object sender, EventArgs e)
{
gvProjectTimeline.DataKeyNames = null;
}
Viewstate is now about one tenth the size. And the page, even with a year's worth of data showing, is on the screen in a flash.
Edit: Have checked a bit more - ViewState is now identical regardless of whether I am showing a week's worth of data or a year's.
Thanks for the suggestion. It would never have occurred to me in a million years to empty the DataKeyNames collection.
Re: ViewState enabled=false no affect?
I've learnt something new too :)
The other thing I was thinking was instead of refrencing/using datakeys in grid.databound event refrence the dataSource with
If DataBinder.Eval(e.Row.DataItem, "ID").ToString() = "xxx" then ......
Re: ViewState enabled=false no affect?
Quote:
Originally Posted by
brin351
I've learnt something new too :)
The other thing I was thinking was instead of refrencing/using datakeys in grid.databound event refrence the dataSource with
If DataBinder.Eval(e.Row.DataItem, "ID").ToString() = "xxx" then ......
I'll give that a try - thanks again.
I'm going to try using html controls too - I habitually use <asp:Label , textbox, dropdownlist etc and populate them from the DataKeyNames collection - if I use html controls I can control the nomenclature and give them very short names. Although the page is now very fast - I do have fast broadband and the page is still pretty huge to be honest. Looking at .net's naming of controls probably explains about half the size of the html.
Re: ViewState enabled=false no affect?
Yep some controls pack alot of punch but there is a price... as Gary aluded to. Fortunately the folks at Microsoft built asp.net with developers in mind.
Re: ViewState enabled=false no affect?
Interesting. Glad to hear that you got it resolved!
Gary