0

I have a massive viewstate hidden field that is causing my application to be unworkable. I have tried:

  • EnableViewState="false" on every control
  • EnableViewState="false" in page directive
  • Page.EnableViewState = false in Page_Init
  • <pages enableViewState="false" /> in web.config

The page causing the issue has a single GridView which I want to render once only, so I don't ever need the viewstate.

I examined the hidden field using this tool, and there is apparently hardly any info in it (since I disabled the property in every control probably). For some reason though, the page insists on including a hidden field that is thousands and thousands of lines long.

How can I get rid of this field (or reduce it to a usable size) for good?


Here is an exert from the offending GridView:

<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False" 
      EnableModelValidation="True" EnableViewState="False" 
      CssClass="my-report">
      <Columns>
          <asp:TemplateField>
              <HeaderTemplate>
                <span title='title' class="abbr">My ID</span>
              </HeaderTemplate>
              <ItemTemplate>
                <%# Eval("my_id") %>
              </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField>
              <HeaderTemplate>
                <span title='title2' class="abbr">Second col heading</span>
              </HeaderTemplate>
              <ItemTemplate>
                  <asp:ListView ID="MyListView" runat="server" EnableViewState="False">
                     <LayoutTemplate>
                        <ul>
                            <asp:PlaceHolder runat="server" ID="itemPlaceHolder" EnableViewState="False" />
                        </ul>
                     </LayoutTemplate>
                     <ItemTemplate>
                            <li><%# Eval("field_2")%></li>
                     </ItemTemplate>
                   </asp:ListView>
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>
Flash
  • 15,945
  • 13
  • 70
  • 98
  • Have you tries stripping it out in the page's Render event? http://stackoverflow.com/questions/2432972/completely-remove-viewstate-for-specific-pages – KodeKreachor Mar 27 '12 at 01:55
  • @KodeKreachor I haven't resorted to that (yet), it seems like a massive kludge. The author of that says _"Overhead of doing this would almost certainly be greater than any possible benefit though"_ which I would have to agree with :( – Flash Mar 27 '12 at 01:58
  • :) Yeah, your options are pretty limited, that's the nature of ASP.NET. That's why I've become a big fan of MVC, it's a powerful framework that delivers a lightweight render, no bloated markup, only what you need. – KodeKreachor Mar 27 '12 at 02:05

4 Answers4

1

The hidden field you see on the page is not only for ViewState, it also contains the ControlState. There is no way to disable the control state so you'll need to find a way to live with it. How many items the grid is displaying?

As a last option you may consider compressing generated viewstate field.

Here you have an MSDN article explaining how ControlState works

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • The grid is displaying a lot of items, and the problem gets worse the more there is. It seems to be occuring because of the ListView I've got inside the last cell of each row. – Flash Mar 27 '12 at 02:26
1

If your GridView is non-interactive (that is, it doesn't contain any child controls that post back), then you can reduce the size of view state by waiting until the page's Render method is called to bind the grid:

Protected Overrides Sub Render(writer As HtmlTextWriter)
    MyGrid.DataSource = ...
    MyGrid.DataBind()
    MyBase.Render(writer)
End Sub
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
0

Another option is to use Flesk.ViewState something.

It can put the viewstate on files, compress it, session, etc.

Like the others say, sometimes is inevitable in ASPNET to live with ViewState.

Thats why your best option is to move to MVC :)

Kat Lim Ruiz
  • 2,425
  • 2
  • 26
  • 32
0

In case anyone has a similar problem, it was occuring because I had a ListView inside each row of the grid. I replaced the ListView with a Repeater and the viewstate is no longer a problem.

Flash
  • 15,945
  • 13
  • 70
  • 98