<p>I need to grab the <code>base64-encoded</code> representation of the <code>ViewState</code>. Obviously, this would not be available until fairly late in the request lifecycle, which is OK.</p>

<p>For example, if the output of the page includes:</p>

<pre><code>&lt;input type="hidden" name="__VIEWSTATE" 
  id="__VIEWSTATE" value="/wEPDwUJODU0Njc5MD...==" /&gt;
</code></pre>

<p>I need a way on the server-side to get the value <code>"/wEPDwUJODU0Njc5MD...=="</code></p>

<p>To clarify, I need this value when the page is being rendered, not on PostBack. e.g. I need to know the ViewState value that is being <em>sent</em> to the client, not the ViewState I'm getting back from them.</p>

[{'answer_id': 1074, 'author': 'Jeff Atwood', 'author_id': 1, 'author_profile': 'https://Stackoverflow.com/users/1', 'pm_score': 5, 'selected': True, 'text': '<p>Rex, I suspect a good place to start looking is solutions that <strong>compress the ViewState</strong> -- they\'re grabbing ViewState on the server before it\'s sent down to the client and gzipping it. That\'s exactly where you want to be.</p>\n\n<ul>\n<li><a href="http://www.hanselman.com/blog/CommentView,guid,febce059-7e7c-439e-af3d-c53d250b3e9c.aspx" rel="noreferrer">Scott Hanselman on ViewState Compression</a> (2005)</li>\n<li><a href="http://www.bloggingdeveloper.com/post/How-To-Compress-ViewState-in-ASPNET-20-ViewState-Compression-with-SystemIOCompression.aspx" rel="noreferrer">ViewState Compression with System.IO.Compression</a> (2007)</li>\n</ul>\n'}, {'answer_id': 1093, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 2, 'selected': False, 'text': '<p>See this <a href="http://aspguy.wordpress.com/2008/07/09/reducing-the-page-size-by-storing-viewstate-on-server/" rel="nofollow noreferrer">blog post</a> where the author describes a method for overriding the default behavior for generating the ViewState and instead shows how to save it on the server Session object.</p>\n\n<blockquote>\n  <p>In ASP.NET 2.0, ViewState is saved by\n  a descendant of PageStatePersister\n  class. This class is an abstract class\n  for saving and loading ViewsState and\n  there are two implemented descendants\n  of this class in .Net Framework, named\n  HiddenFieldPageStatePersister and\n  SessionPageStatePersister. By default\n  HiddenFieldPageStatePersister is used\n  to save/load ViewState information,\n  but we can easily get the\n  SessionPageStatePersister to work and\n  save ViewState in Session object.</p>\n</blockquote>\n\n<p>Although I did not test his code, it seems to show exactly what you want: a way to gain access to ViewState code while still on the server, before postback. </p>\n'}, {'answer_id': 2058, 'author': 'Dave Anderson', 'author_id': 371, 'author_profile': 'https://Stackoverflow.com/users/371', 'pm_score': 2, 'selected': False, 'text': '<p>I enabled compression following similar articles to those posted above. The key to accessing the ViewState before the application sends it was overriding this method;</p>\n\n<pre><code>protected override void SavePageStateToPersistenceMedium(object viewState)\n</code></pre>\n\n<p>You can call the base method within this override and then add whatever additional logic you require to handle the ViewState.</p>\n'}]
['https://Stackoverflow.com/questions/1010', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/67/']