Results 1 to 18 of 18

Thread: ViewState enabled=false no affect?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by benmartin101 View Post
    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.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    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.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by sapator View Post
    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by brin351 View Post
    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?

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: ViewState enabled=false no affect?

    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?
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by brin351 View Post
    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.

  11. #11
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    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.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by gep13 View Post
    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.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by brin351 View Post
    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.
    Last edited by Webskater; Sep 19th, 2011 at 05:48 PM.

  15. #15
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    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 ......
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: ViewState enabled=false no affect?

    Quote Originally Posted by brin351 View Post
    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.

  17. #17
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    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.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  18. #18
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ViewState enabled=false no affect?

    Interesting. Glad to hear that you got it resolved!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width