<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WebPaws.com &#187; viewstate</title>
	<atom:link href="http://webpaws.com/blog/tag/viewstate/feed/" rel="self" type="application/rss+xml" />
	<link>http://webpaws.com/blog</link>
	<description>Internet Marketing and Design</description>
	<lastBuildDate>Tue, 15 Jun 2010 11:43:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>PostBack in ASP.net &#8211; passing variables on postback</title>
		<link>http://webpaws.com/blog/5/postback-in-aspnet-passing-variables-on-postback/</link>
		<comments>http://webpaws.com/blog/5/postback-in-aspnet-passing-variables-on-postback/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 21:22:35 +0000</pubDate>
		<dc:creator>webpaws</dc:creator>
				<category><![CDATA[ASP]]></category>
		<category><![CDATA[postback]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[viewstate]]></category>

		<guid isPermaLink="false">http://webpaws.com/blog/?p=5</guid>
		<description><![CDATA[Be aware that if you set [...]<br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://webpaws.com/blog/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br /><p><a href="http://webpaws.com/blog/5/postback-in-aspnet-passing-variables-on-postback/">PostBack in ASP.net &#8211; passing variables on postback</a> is a post from: <a href="http://webpaws.com/blog">WebPaws.com</a></p>
]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>Be aware that if you set a <span class="highlight">variable</span> before <span class="highlight">postback</span> and then check it after <span class="highlight">postback</span> it will be reset, use Session or ViewState to keep <span class="highlight">variable</span> values regardless of <span class="highlight">postback</span> state.</p>
<p>ViewState is a collection bag which holds key value pairs of changed control attributes. You can also utilize it to store your own values which will persist though a Page&#8217;s postbacks. ViewState is a member of Page, which means it is globably accessible throughout your Page. The ViewState is loaded early on in the Page creation (life cycle), and towards the end it is encrypted and outputed into the HTML. Therefore, when you add some data to the ViewState on the first page load, that data is being stored (encrypted) in the __ViewState hidden field you&#8217;ll find in the HTML source.  When <span style="text-decoration: underline;">t</span>he page is loaded again (after you click the button), that value is decrypted and loaded into the ViewState bag, which means you&#8217;ll be able to access it.</p>
<p>Remember to always check if the value you are getting (either from ViewState or Session) is null or not. If not, you&#8217;re users will get the infamous &#8220;Object reference not sent to an instance of an object.&#8221; exception.</p>
<p>ViewState is the mechanism that allows state values to be preserved across page postbacks.</p>
<p>Because of the stateless nature of web pages, regular page member variables will not maintain their values across postbacks.  When we need a page variable to maintain its value across page post backs, we can use ViewState to store that value.  Values stored in ViewState will be serialized and sent to the client browser as the value of a hidden form input.  When you view the page source (in your browser) of a page the uses ViewState, you may see this hidden viewstate input which will look something like this:</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;__VIEWSTATE&#8221; id=&#8221;__VIEWSTATE&#8221; value=&#8221;/wQPDwAKMTn1ODM5Rj&#8230;&#8230;.&#8221; /&gt;</p>
<p>This single hidden field contains all the viewstate values for all the page controls. This is an important aspect of viewstate that you need to consider.</p>
<h3>Example</h3>
<p>One simple way to store small values in viewstate is to use a property instead of a member variable.  This property can use viewstate to store its value rather than a member variable that would lose the value over a postback. For example, storing an Integer in viewstate can be accomplished like this:</p>
<p>VB</p>
<div style="padding: 5px; color: black; font-family: Courier New; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p style="margin: 0px;"><span style="color: blue;">Public</span> <span style="color: blue;">Property</span> SomeInteger() <span style="color: blue;">As</span> <span style="color: blue;">Integer</span></p>
<p style="margin: 0px;"><span style="color: blue;">Get</span></p>
<p style="margin: 0px;"><span style="color: blue;">Dim</span> o <span style="color: blue;">As</span> <span style="color: blue;">Object</span> = ViewState(<span>&#8220;SomeInteger&#8221;</span>)</p>
<p style="margin: 0px;"><span style="color: blue;">If</span> <span style="color: blue;">Not</span> o <span style="color: blue;">Is</span> <span style="color: blue;">Nothing</span> <span style="color: blue;">Then</span> <span style="color: blue;">Return</span> <span style="color: blue;">DirectCast</span>(o, <span style="color: blue;">Integer</span>)</p>
<p style="margin: 0px;"><span style="color: blue;">Return</span> 0 <span style="color: green;">&#8216;a default</span></p>
<p style="margin: 0px;"><span style="color: blue;">End</span> <span style="color: blue;">Get</span></p>
<p style="margin: 0px;"><span style="color: blue;">Set</span>(<span style="color: blue;">ByVal</span> value <span style="color: blue;">As</span> <span style="color: blue;">Integer</span>)</p>
<p style="margin: 0px;">ViewState(<span>&#8220;SomeInteger&#8221;</span>) = value</p>
<p style="margin: 0px;"><span style="color: blue;">End</span> <span style="color: blue;">Set</span></p>
<p style="margin: 0px;"><span style="color: blue;">End</span> <span style="color: blue;">Property</span></p>
<p style="margin: 0px;">
</div>
<br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://webpaws.com/blog/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br /><p><a href="http://webpaws.com/blog/5/postback-in-aspnet-passing-variables-on-postback/">PostBack in ASP.net &#8211; passing variables on postback</a> is a post from: <a href="http://webpaws.com/blog">WebPaws.com</a></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://webpaws.com/blog/5/postback-in-aspnet-passing-variables-on-postback/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
