<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Me Myself &#38; C#</title>
	<atom:link href="http://gargmanoj.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gargmanoj.wordpress.com</link>
	<description>Manoj Garg's Tech Bytes - What I learned Today</description>
	<lastBuildDate>Tue, 27 Nov 2012 13:46:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gargmanoj.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/ef5fb8d61583d64951ce173271967f38?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Me Myself &#38; C#</title>
		<link>http://gargmanoj.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gargmanoj.wordpress.com/osd.xml" title="Me Myself &#38; C#" />
	<atom:link rel='hub' href='http://gargmanoj.wordpress.com/?pushpress=hub'/>
		<item>
		<title>My experiences with TFS: Upgrade from TFS 2010 Beta 1 to TFS 2010 RC</title>
		<link>http://gargmanoj.wordpress.com/2010/03/11/my-experiences-with-tfs-upgrade-from-tfs-2010-beta-1-to-tfs-2010-rc/</link>
		<comments>http://gargmanoj.wordpress.com/2010/03/11/my-experiences-with-tfs-upgrade-from-tfs-2010-beta-1-to-tfs-2010-rc/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 18:14:23 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Beta1 to RC]]></category>
		<category><![CDATA[Team Foundation Server]]></category>
		<category><![CDATA[TFS 2010]]></category>
		<category><![CDATA[upgrade]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/?p=221</guid>
		<description><![CDATA[Since last few days I have been playing with TFS. So, I expect my next few posts on my experiences with it. Let&#8217;s starts with installing TFS 2010. To get a feel of it, &#8220;What this is all about&#8221;, we decided to install a trial version of TFS 2010. MS has release 3 versions of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=221&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since last few days I have been playing with TFS. So, I expect my next few posts on my experiences with it. Let&#8217;s starts with installing TFS 2010.
</p>
<p>To get a feel of it, &#8220;What this is all about&#8221;, we decided to install a trial version of TFS 2010.
</p>
<p>MS has release 3 versions of TFS 2010 so far, from Beta1 <span style="font-family:Wingdings;">è</span> Beta 2 <span style="font-family:Wingdings;">è</span> RC. I don&#8217;t know why we downloaded Beta1 despite latest being available there. For the first time, it took my good 5-6 hrs to have a running TFS instance. This included installing SQL Server 2008, Team explorer, Power tools 2008 etc etc. There are many posts available over net to guide you through the installation process like <a href="http://aaronwball.blogspot.com/2009/06/installing-team-foundation-server-2010.html">this</a>, <a href="http://www.jeffreyhunsaker.com/2009/05/installing-tfsvsts-2010-beta-1.html">this</a>, <a href="http://juliocasal.com/archive/2009/11/02/team-foundation-server-2010-beta-2-office-sharepoint-server-2007-single-server-installation-part-1.aspx">this</a> and <a href="http://juliocasal.com/archive/2009/11/02/team-foundation-server-2010-beta-2-office-sharepoint-server-2007-single-server-installation-part-2.aspx">this</a>.
</p>
<p>While trying to learn it, I came to know that a lot has been changed between Beta 1 and RC, specially the TFS SDK APIs. As my main motive behind learning TFS was to write a 3<sup>rd</sup> party application which uses APIs exposed by TFS SDK to pull information from TFS instance and use it, I decided to upgrade my TFS instance to RC version.
</p>
<p>Before upgrading the installation, I searched over web to see if there are some links available which explain this procedure but couldn&#8217;t find anything. All the post, forums were talking about upgrade from Beta 2 to RC. <a href="http://blogs.microsoft.co.il/blogs/shair/archive/2010/02/10/upgrade-team-system-beta-2-to-rc-smooth.aspx">This</a> article from Shai, has explained the upgrade process from Beta 2 to RC quite well. I thought of following the same step for Beta 1 instance.
</p>
<p>I started with uninstalling TFS 2010 beta 1 and some other products which were installed along with it like VS 2010 Shell, Power tools 2008, Team Explorer 2010 Beta1. Then I started installer for RC, which showed a list of new programs that it was about to install as shown in figure below.
</p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc1.png">
	</p>
<p>Once installation of above mentioned component was complete, I started &#8220;TFS foundation Server Configuration Manger&#8221; from program menu and opted for <strong>upgrade</strong> of my existing TFS 2010 beta1 instance.
</p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc2.png">
	</p>
<p>
 </p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc3.png">
	</p>
<p>After providing all the details as shown in figure above, installer asked for verifying the configuration setting. After clicking the &#8220;Verify&#8221; button it gave an error saying that &#8220;TFS 2010 beta1 can&#8217;t be upgraded to RC&#8221;. I was like &#8220;What&#8221;. Error message is shown in figure below.
</p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc4.png">
	</p>
<p>Since I had already uninstalled Beta 1 and didn&#8217;t have setup for Beta 2. So I had no option other than installing RC from scratch. So started TFS Configuration manger and selected for &#8220;Standard Configuration on Single server&#8221; option for installation. System presented me with the following welcome screen.
</p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc5.png">
	</p>
<p>For here on I again provided configuration details for new TFS system as shown in figure below. After verification of all these details, TFS setup was complete.
</p>
<p><img src="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc6.png">
	</p>
<p>Once installation of TFS RC was complete, also installed Team Explorer and TFS Power Tools 2010 RC.
</p>
<p>This way my TFS setup was complete.</p>
<br />Filed under: <a href='http://gargmanoj.wordpress.com/category/uncategorized/'>Uncategorized</a> Tagged: <a href='http://gargmanoj.wordpress.com/tag/beta1-to-rc/'>Beta1 to RC</a>, <a href='http://gargmanoj.wordpress.com/tag/team-foundation-server/'>Team Foundation Server</a>, <a href='http://gargmanoj.wordpress.com/tag/tfs-2010/'>TFS 2010</a>, <a href='http://gargmanoj.wordpress.com/tag/upgrade/'>upgrade</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=221&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2010/03/11/my-experiences-with-tfs-upgrade-from-tfs-2010-beta-1-to-tfs-2010-rc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc1.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc2.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc3.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc4.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc5.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2010/03/031110_1813_myexperienc6.png" medium="image" />
	</item>
		<item>
		<title>Design patterns: Strategy v/s Visitor</title>
		<link>http://gargmanoj.wordpress.com/2010/02/04/design-patterns-strategy-vs-visitor/</link>
		<comments>http://gargmanoj.wordpress.com/2010/02/04/design-patterns-strategy-vs-visitor/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 18:09:21 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Strategy pattern]]></category>
		<category><![CDATA[Visitor Pattern]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/?p=205</guid>
		<description><![CDATA[Design patterns are recommended solution for some well-defined problems with a predefined context. As Wikipedia says: &#8220;A &#8216;Design Pattern&#8217; is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=205&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-size:12pt;">Design patterns are recommended solution for some well-defined problems with a predefined <strong><em>context</em></strong>. As <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">Wikipedia</a> says:<br />
</span></p>
<blockquote><p><span style="font-size:12pt;"><em>&#8220;A &#8216;Design Pattern&#8217; is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.&#8221;<br />
</em></span></p></blockquote>
<p><span style="font-size:12pt;">Context is important while deciding which pattern to use in a given situation. There may be more than one qualifying pattern for a problem but all of them may or may not be suitable in that particular situation.<br />
</span></p>
<p><span style="font-size:12pt;">Some time back, I had implemented Visitor pattern for a problem. When a colleague of mine was reviewing it, we got into a discussion that this was best fit for a Strategy rather than Visitor pattern. That got me searching around for the right context and difference between these two so similar but yet so different pattern.<br />
</span></p>
<p><span style="color:black;font-size:12pt;">In visitor pattern the object structure accepts a visitor and performs visit on it. In strategy the only difference I could notice was that the composite or object structure composes a strategy object and calls Algorithm. Aren&#8217;t these two patterns essentially the same? Or rather can I put it like strategy is a cut down version of visitor or a visitor on a single type of element?<br />
</span></p>
<blockquote><p><span style="font-size:12pt;"><em>In the <a href="http://c2.com/cgi/wiki?StrategyPattern" target="_blank"><span style="color:#2a5db0;">StrategyPattern</span></a>, the context points to a strategy and delegates to it. There is no context in the <a href="http://c2.com/cgi/wiki?VisitorPattern" target="_blank"><span style="color:#2a5db0;">VisitorPattern</span></a> and the visitor is often created each time it is used. The <a href="http://c2.com/cgi/wiki?VisitorPattern" target="_blank"><span style="color:#2a5db0;">VisitorPattern</span></a> is based on DoubleDispatching, and there is none in the <a href="http://c2.com/cgi/wiki?StrategyPattern" target="_blank"><span style="color:#2a5db0;">StrategyPattern</span></a>. As far as I can see, these patterns have little to do with each other</em>.   &#8211;<a href="http://c2.com/cgi/wiki?RalphJohnson" target="_blank"><span style="color:#2a5db0;">RalphJohnson</span></a><br />
</span></p></blockquote>
<p><span style="color:black;font-size:12pt;"><strong>Visitor</strong></span><br />
<span style="color:black;font-size:12pt;"><br />
The intent of the visitor pattern is to add operations to a class which has rich data structure. If we are not able to identify all the operations in for the class but we expect in future we may need to add method for the class, but it is not advisable to alter the class, so to be on safer side we can put a visitor and share the data to the visitor class</span></p>
<p><strong>Strategy</strong><br />
<span style="color:black;font-size:12pt;"><br />
The intent of strategy pattern is to define a family of algorithm, encapsulate each one, and make them interchangeable. Let the algorithm vary independently from the clients that use it. This pattern is used to add different algorithm nothing to do with the data in the class.</span></p>
<p><a href="http://leedrickdotnet.blogspot.com/2007/01/strategy-pattern-vs-visitor-pattern.html"><span style="font-size:12pt;">Here</span></a><span style="color:black;font-size:12pt;"> is an article by LEEDRICK discussing difference and similarities in these two patterns.<br />
</span></p>
<p><span style="font-size:12pt;">Please feel free to reply with your suggestions. </span></p>
<br />Filed under: <a href='http://gargmanoj.wordpress.com/category/design-patterns/'>Design Patterns</a> Tagged: <a href='http://gargmanoj.wordpress.com/tag/strategy-pattern/'>Strategy pattern</a>, <a href='http://gargmanoj.wordpress.com/tag/visitor-pattern/'>Visitor Pattern</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=205&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2010/02/04/design-patterns-strategy-vs-visitor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>Encrypting entries in ASP.NET Web.Config</title>
		<link>http://gargmanoj.wordpress.com/2009/12/09/encrypting-entries-in-asp-net-web-config/</link>
		<comments>http://gargmanoj.wordpress.com/2009/12/09/encrypting-entries-in-asp-net-web-config/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 11:44:28 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataProtectionConfigurationProvider]]></category>
		<category><![CDATA[DPAPI]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[RSAConfigurationProvider]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/12/09/encrypting-entries-in-asp-net-web-config/</guid>
		<description><![CDATA[One of the best practices suggested while working with ASP.NET application is keeping values you want to make plug and play (changing them without changing the implementation) in web.config file. These configurable values can be anything ranging from some string constants to connection strings. Almost all the developers(at least those I know) follow this suggestion [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=202&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-family:Candara;font-size:12pt;">One of the best practices suggested while working with ASP.NET application is keeping values you want to make plug and play (changing them without changing the implementation) in web.config file. These configurable values can be anything ranging from some string constants to connection strings. Almost all the developers(at least those I know) follow this suggestion religiously. We keep these values in a readable format in web.config. Sometimes these values can have sensitive information like if we are storing connection string to our production database and if SQL server credentials are used to login to database server, then connection string will have username and password for the production database. Keeping credentials for database server unguarded can be a huge risk. Though IIS does a good job by  blocking access to any resource with .config extension by default. But certainly there are many ways (leaving FTP open <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) by which someone with not so good intensions gets access to this config file containing credentials to database server. So, if one has this kind of sensitive data in config file then that data should be stored in encrypted way to mitigate risk of credentials falling under wrong hands.<br />
</span></p>
<p><span style="font-family:Candara;font-size:12pt;">This topic popped up while having a discussion with a colleague. He mentioned that .NET has some way of encrypting the values in config files. Which prompted me to search about this feature and Yes, .NET provides a very simple but effective way to encrypt values in config files. As you might already know, configuration settings in .NET work in <a href="http://msdn.microsoft.com/en-us/library/ms972319.aspx">provider model</a> i.e. these settings can be replaced with another values without impacting the implementation.<br />
</span></p>
<p><span style="font-family:Candara;font-size:12pt;">.NET framework provides 2 encryption providers for encrypting the config file values.<br />
</span></p>
<ol>
<li><span style="font-family:Candara;font-size:12pt;"><strong><em>DataProtectionConfigurationProvider</em></strong> : It uses <strong><em>Windows DPAPI</em></strong>s(Data Protection API) to encrypt the data in config file and the key used to decrypt the encrypted values in stored in windows <strong><em>Local Security Authority (LSA)</em></strong>.<br />
</span></li>
<li><span style="font-family:Candara;font-size:12pt;"><strong><em>RSAProtectedConfigurationProvider</em></strong> : This provider uses public key encryption approach provided by <strong><em>RSACryptoProvider</em></strong> in .NET. This public key is stored in the config file itself.<br />
</span></li>
</ol>
<p><span style="font-family:Candara;font-size:12pt;">To encrypt a configuration section use <a href="http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.protectsection.aspx">ConfigurationSection.SectionInformation.ProtectSection(providername)</a> method and to decrypt the values use <a href="http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.unprotectsection.aspx">ConfigurationSection. SectionInformation.UnProtectSection()</a> method.<br />
</span></p>
<p><span style="font-family:Candara;font-size:12pt;">Once you have encrypted the configuration sections, config file will no longer show the actual values instead it will be showing some cipher text. But at the time of accessing these values in code (C# or inline in aspx page) you don&#8217;t have to do anything like decrypting the config file or something, .NET will do it for you. But just take precaution while encrypting the config file values as this auto decryption done by .NET has some performance implications as well. So one should take a wise decision about which data to encrypt and which data can be written in plain text.<br />
</span></p>
<p><span style="font-family:Candara;font-size:12pt;">There are plethora of resources on this topic on web and my this post will also add to crowd, but following few link I found very informative and to the point.<br />
</span></p>
<ol>
<li><a href="http://www.4guysfromrolla.com/articles/021506-1.aspx"><span style="font-family:Candara;font-size:12pt;">Encrypting Configuration Information in ASP.NET 2.0 Applications</span></a><span style="font-family:Candara;font-size:12pt;"><br />
			</span></li>
<li><a href="http://ondotnet.com/pub/a/dotnet/2005/02/15/encryptingconnstring.html"><span style="font-family:Candara;font-size:12pt;">Encrypting Connection Strings in Web.config</span></a><span style="font-family:Candara;font-size:12pt;"><br />
			</span></li>
</ol>
<br />Posted in .Net 2.0, ASP.Net, C# Tagged: DataProtectionConfigurationProvider, DPAPI, encryption, RSAConfigurationProvider, web.config <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/202/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=202&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/12/09/encrypting-entries-in-asp-net-web-config/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>DOM property comparison for different browsers: What Works Where?</title>
		<link>http://gargmanoj.wordpress.com/2009/11/25/dom-property-comparison-for-different-browsers-what-works-where/</link>
		<comments>http://gargmanoj.wordpress.com/2009/11/25/dom-property-comparison-for-different-browsers-what-works-where/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 13:09:46 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[browser compatibility]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[innerText]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[textContent]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/11/25/dom-property-comparison-for-different-browsers-what-works-where/</guid>
		<description><![CDATA[Today, I was working on a UI enhancement task where I had to do some DOM manipulation. Basically task was changing color of some SPAN elements in DOM depending on their value like if it&#8217;s value is XXX then show in RED color and so on. As always I was using IE to test my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=199&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today, I was working on a UI enhancement task where I had to do some DOM manipulation. Basically task was changing color of some SPAN elements in DOM depending on their value like if it&#8217;s value is XXX then show in RED color and so on. As always I was using IE to test my Java script code written to do the job. In JS code, I used &#8220;innerText&#8221; property of SPAN element to get the text of that element and then did the comparison to change the color and It worked fine. When I ran same JS code on Mozilla, it didn&#8217;t run (I mean it got executed but didn&#8217;t changed color of SPAN elements to RED <span style="font-family:Wingdings;">L</span>) . On debugging the script, I was surprised to see value of <em>innerText</em> property being returned <strong><em>null</em></strong>. When googled around it and found that only Mozilla doesn&#8217;t support this property <span style="font-family:Wingdings;">L</span>. Mozilla has another property &#8220;textContent&#8221; which has same value as innerText in other browsers.
</p>
<p>Solution was simple, use an ORing of both the values instead of putting if-else block in code to use <em>textContent</em> if the user is running Mozilla or innerText for any other browser.
</p>
<p>var txtName = clientNameElement.innerText || clientNameElement.textContent;
</p>
<p>I picked up this solution from <a href="http://www.quirksmode.org/dom/w3c_html.html">here</a>. This link provides a good comparison of various DOM properties on different browsers that is what works and what not <span style="font-family:Wingdings;">J</span>.
</p>
<p>Hope it helps some of you while writing code to cater to multiple browsers <span style="font-family:Wingdings;">J</span>
	</p>
<br />Posted in JavaScript Tagged: browser compatibility, DOM, IE, innerText, Mozilla, textContent <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=199&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/11/25/dom-property-comparison-for-different-browsers-what-works-where/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>Blogging right from Word 2007</title>
		<link>http://gargmanoj.wordpress.com/2009/09/22/blogging-right-from-word-2007/</link>
		<comments>http://gargmanoj.wordpress.com/2009/09/22/blogging-right-from-word-2007/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 11:24:35 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[Microsoft Word 2007]]></category>
		<category><![CDATA[Non Technical]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[blogging from word 2007]]></category>
		<category><![CDATA[office 2007]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/?p=178</guid>
		<description><![CDATA[I have been using Microsoft Office 2007 since last one and half years and frankly speaking &#8220;I&#8217;m lovin&#8217; it&#8221;. Be it quick style feature of Word 2007 or ShapeStyle, WordArt Style of Powerpoint 2007, I have been using them extensively in this period. The new interface is so usable at the same time providing me [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=178&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p style="text-align:justify;">I have been using Microsoft Office 2007 since last one and half years and frankly speaking &#8220;I&#8217;m lovin&#8217; it&#8221;. Be it quick style feature of Word 2007 or ShapeStyle, WordArt Style of Powerpoint 2007, I have been using them extensively in this period. The new interface is so usable at the same time providing me lots of, out of the box available formatting features like table style, document templates n so on.
</p>
<p style="text-align:justify;">Ok so enough of praises for Office 2007, after all this post is not about why I like it. There are plethora of resources, posts, videos available over net describing the above mention benefits (features) of office 2007.  This post is about creating and publishing a blog entry right from Word 2007 document. Since last few days I have been trying to be little active with my blog and write something to do justice with the its tagline &#8220;<strong><em>What I learned today</em></strong>&#8220;. Last week I was reading <a href="http://phapalegaurav.blogspot.com/2009/09/test-post.html">Gaurav&#8217;s blog</a> and there he had a post about blogging from Word itself. So far I have been using Windows Live Writer to do most of my blogging related stuff(which is minimal BTW <span style="font-family:Wingdings;">J</span>), which is a great tool for blogging but it didn&#8217;t allowed me to format my post the way I wanted them to for ex putting quotes, creating colored tables for comparison related stuff etc. After reading gaurav&#8217;s post, I myself thought to give a spin to this feature of publishing content from word.
</p>
<p style="text-align:justify;">So here is the post for my tryst with blogging in Word 2007.
</p>
<h2>Enable publishing form word<br />
</h2>
<p>
 </p>
<div>
<table style="border-collapse:collapse;" border="0">
<col>
<col>
<tbody valign="top">
<tr>
<td style="padding-left:7px;padding-right:7px;">
<p style="text-align:justify;"><img src="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig11.png"></p>
</td>
<td style="padding-left:7px;padding-right:7px;">
<p style="text-align:justify;">Go to Office Button in word and then select Blog option for Publish menu item.
</p>
<p style="text-align:justify;">This would ask you to register a blog account with MS Word. Register your blog by providing the details about your blog as shown in below screenshots.
</p>
<p style="text-align:justify;">
 </p>
<p style="text-align:justify;"><img src="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig21.png">
						</p>
<p style="text-align:justify;">
 </p>
<p style="text-align:justify;"><img src="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig31.png">
						</p>
<p style="text-align:justify;">
 </p>
<p style="text-align:justify;"><img src="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig41.png">
						</p>
</td>
</tr>
</tbody>
</table>
</div>
<p style="text-align:justify;">
 </p>
<p style="text-align:justify;">After registering the blog a new word document will open and you are good to go. I tried various formatting setting, picture formatting. So far so good. I am going to use word now instead of Livewriter.</p>
<br />Posted in Microsoft Word 2007, Non Technical Tagged: blog, blogging from word 2007, office 2007 <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=178&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/09/22/blogging-right-from-word-2007/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>

		<media:content url="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig11.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig21.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig31.png" medium="image" />

		<media:content url="http://gargmanoj.files.wordpress.com/2009/09/092209_1123_bloggingrig41.png" medium="image" />
	</item>
		<item>
		<title>ViewState Compression in ASP.NET 2.0</title>
		<link>http://gargmanoj.wordpress.com/2009/09/16/viewstate-compression-in-asp-net-2-0/</link>
		<comments>http://gargmanoj.wordpress.com/2009/09/16/viewstate-compression-in-asp-net-2-0/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 15:26:10 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ViewState]]></category>
		<category><![CDATA[.browser]]></category>
		<category><![CDATA[ControlState]]></category>
		<category><![CDATA[IIS compression]]></category>
		<category><![CDATA[LoadPageStateFromPersistenceMedium]]></category>
		<category><![CDATA[PageAdapter]]></category>
		<category><![CDATA[PageStatePersister]]></category>
		<category><![CDATA[RequiresControlStateInSession]]></category>
		<category><![CDATA[SavePageStateToPersistenceMedium]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Viewstate compression]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/?p=161</guid>
		<description><![CDATA[&#8220;STATE as a noun can be described as &#8220;the condition of a person or thing, as with respect to circumstances or attributes&#8220;.  In ASP.NET world, State of a control or a page is what it looks like, what its value is and what its value was before the page was posted back.&#8220; Since HTTP is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=161&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<blockquote><p><span style="font-size:small;"><span style="font-family:Calibri;"><span style="font-size:large;">&#8220;</span><span style="font-family:georgia;font-size:small;">STATE as a noun can be described as &#8220;<em>the condition of a person or thing, as with respect to circumstances or attributes</em>&#8220;.  In ASP.NET world, State of a control or a page is what it looks like, what its value is and what its value was before the page was posted back</span>.<span style="font-size:large;">&#8220;</span></span></span></p></blockquote>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">Since HTTP is stateless, to maintain state across postbacks in ASP.NET, some state management techniques are used. In ASP.NET state management can be categorized in two types depending on the location where state is being kept:</span></span></p>
<ol>
<li>
<div><span style="font-size:x-small;"><span style="font-family:georgia;"><span style="font-family:'Cambria Math';"><span style="font-size:small;"><strong>Client Side</strong>: <span style="font-family:Georgia;">Client side state management involves keeping the state data on the client end, either on client machine or the browser. There are various mechanisms to maintain state on client side like<em> Cookies, ViewState, Hidden Fields, Control State, Query strings</em> etc.</span> </span></span></span></span></div>
</li>
<li>
<div><span style="font-family:ge;"><span style="font-size:x-small;"><span style="font-size:small;"><span style="font-family:'Cambria Math';"><span style="font-family:georgia;"><strong>Server Side</strong>: Server side management is keeping state on a different machine be it database or file system on a different computer. Server side state management can be done using Application State, Session state etc. this data can be  stored in the server process or in a database or server file system.</span> </span></span></span></span></div>
</li>
</ol>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">For detailed information about different option available for state management in ASP.NET, please refer </span></span><a href="http://msdn.microsoft.com/en-us/library/75x4ha6s(printer).aspx" target="_blank"><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">this</span></span></a><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;"> link for MSDN. </span></span><a href="http://www.dotnetfunda.com/articles/article61.aspx" target="_blank"><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">This </span></span></a><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">link also gives a good comparison of various state management techniques with  their pros n cons.</span></span></p>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">ViewState is the first thing that come to mind when dealing with postbacks in ASP.NET.</span></span></p>
<h2>ViewState</h2>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">In simple terms, ViewState is keeping information about page in a non-readable i.e. serialized way with the page itself. In ASP.NET, view state is kept in a hidden field name _VIEWSTATE on the page. So every time the page loads after the postback this serialized data is read and appropriate state is loaded into the controls.</span></span></p>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">To understand how viewstate is saved and loaded, one should know the page life cycle i.e. the steps a request, for loading a page, goes through before a page is fully rendered into the browser. </span></span><a href="http://msdn.microsoft.com/en-us/library/ms972976(printer).aspx" target="_blank"><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">This </span></span></a><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">link on MSDN explains Page life cycle along with a good overview of what view state is. </span><a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx" target="_blank"><span style="font-size:small;">This</span></a><span style="font-size:small;"> post by <em>Dave Reed</em> gets under the hood of view state and explains the nuances of view state very well. I would recommend reading the above mention post to every one. That post has been the best link I have come across on view state.</span></span></p>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">As mentioned above, View state is kept in a hidden field _VIEWSTATE on the page. So every time the page is posted back to the server or retrieved from server, the content of View state also travel along with page content. In turn contributing to usage of network resources. Generally, developers have tendency to keep view state enabled for controls on their page. which unknowingly keeps growing the page size. This means as page size grows, it would take longer for the page to completely render itself. </span></span></p>
<p><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">Problem comes when the page size grows inordinately and waiting time for the end user to see the completely rendered page is too high. There are recommendation that one should use viewstate as minimally as possible to ensure the page size is with in limits. But sometime scenario enforces the developer to use viewstate in his/her page. In such cases following are some of the guideline/approaches to keep view state size in check.</span></span></p>
<ol>
<li>
<div><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;"><strong>Enable view state for a control only when absolutely necessary</strong>: Set the EnableViewState attribute for the control to <em>false</em> explicitly, when you are sure that you do not require to keep state for that control.</span></span></div>
</li>
<li>
<div><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;"><strong>Don’t save large datasets into view state</strong>: Whenever possible keep the datasets and big data tables in cache or say session so that view state is small.</span></span></div>
</li>
<li>
<div><span style="font-size:x-small;"><span style="font-family:georgia;"><span style="font-size:small;"><strong>Use IIS Compression</strong>: Internet Information Services(IIS) has the feature to provide compression for the pages it serves as response to the requests received. You can configure the IIS on your server to compress the appropriate websites or pages in the website. For detailed information about how to configure the IIS configuration on your server, please read </span></span></span><a href="http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx" target="_blank"><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;">this</span></span></a><span style="font-family:georgia;"><span style="font-size:small;"><span style="font-size:x-small;"> <span style="font-size:small;">post by </span></span><span style="font-size:x-small;"><span style="font-size:small;"><em>Scott Forsyth</em>.  There is one another </span><a href="http://www.codeproject.com/KB/dotnet/DotNetBestPractices4.aspx" target="_blank"><span style="font-size:small;">post</span></a><span style="font-size:small;"> by <em>Shivprasad koirala</em>, which is an extension of the post by <em>Scott Forsyth</em>, it also explains IIS compression in great details along with it also provides some comparison statistics data about what level of compression is good and for what type of pages one should avail this</span> </span></span></span></div>
</li>
<li>
<div><span style="font-family:georgia;font-size:x-small;"><span style="font-size:small;"><strong>Using old ASP.NET 1.1 approach for storing the view state on server</strong>: In Page life cycle there are two steps available which do the job of fetching the view state from the hidden field and putting the view state content back into the hidden field it was stored. These methods are </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.loadpagestatefrompersistencemedium.aspx" target="_blank"><span style="font-size:small;">LoadPageStateFromPersistenceMedium</span></a><em> </em><span style="font-size:small;"> and <em><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.savepagestatetopersistencemedium.aspx" target="_blank">SavePageStateToPersistenceMedium</a>. </em>These methods are virtual methods from the Page class. A simplest approach would be to store the viewstate in session itself. Following code snippet shows how this could be done by overriding the two above mentioned methods and storing the viewstate in session after serializing and loading the viewstate from session again after desterilizing it. Serialization and deserialization is done using </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.losformatter.aspx" target="_blank"><span style="font-size:small;">LosFormatter</span></a><span style="font-size:small;"> class. One can use any of the available formatters, </span><a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx" target="_blank"><span style="font-size:small;">BinaryFormatter</span></a><span style="font-size:small;">, </span><a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter.aspx" target="_blank"><span style="font-size:small;">SOAPFormatter</span></a><span style="font-size:small;"> etc., for serializing the viewstate content. Content is stored in session with SessionID being the key.</span></span></div>
</li>
</ol>
<div style="background:#fbfbfb;margin-left:.5in;margin-right:0;border:#cecece 1pt solid;padding:4pt;">
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;"><span style="color:blue;" lang="EN">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">object</span> LoadPageStateFromPersistenceMedium ()</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">{</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">    <span style="color:blue;">return</span> (<span style="color:blue;">new</span> LosFormatter().Deserialize ((<span style="color:blue;">string</span>)Session[Session.SessionID]));</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">}
</span><span style="font-size:x-small;"><span style="color:blue;" lang="EN">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> SavePageStateToPersistenceMedium (<span style="color:blue;">object</span> state)</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">{</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">    LosFormatter los = <span style="color:blue;">new</span> LosFormatter();</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">    los.Serialize (sw, state);</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">    <span style="color:blue;">string</span> vs = sw.ToString ();</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">    Session[Session.SessionID] = vs;</span></pre>
<pre style="background:#fbfbfb;border:medium none;padding:0;"><span style="font-size:x-small;">}</span></pre>
</div>
<p><span style="font-family:'Cambria Math';font-size:small;"> </span></p>
<p><span style="font-family:Georgia;font-size:small;">An improvement in above mentioned approach could be to apply the compression on the viewstate content before storing them in session and decompressing the session content before restoring the viewstate. Following code does exactly this. This code introduces one more step, between storing and retrieving the view state from session, by compressing and decompressing the content. This approach again leads to save some more bandwidth.</span><span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre;"> </span></p>
<p><span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre;">Following class contains method for compression and decompression. Source code from <a href="http://www.dreamincode.net/code/snippet1717.htm">http://www.dreamincode.net/code/snippet1717.htm</a></span></p>
<pre>                                                                    

using System.IO;
using System.IO.Compression;

public static class Compressor
{
/// &lt;summary&gt;
/// Method for compressing the ViewState data
/// &lt;/summary&gt;
/// &lt;param name="data"&gt;ViewState data to compress&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static byte[] Compress(byte[] data)
{
    //create a new MemoryStream for holding and
    //returning the compressed ViewState
    MemoryStream output = new MemoryStream();
    //create a new GZipStream object for compressing
    //the ViewState
    GZipStream gzip = new GZipStream(output,CompressionMode.Compress, true);
    //write the compressed bytes to the underlying stream
    gzip.Write(data, 0, data.Length);
    //close the object
    gzip.Close();
    //convert the MemoryStream to an array and return
    //it to the calling method
    return output.ToArray();
}

/// &lt;summary&gt;
/// Method for decompressing the ViewState data
/// &lt;/summary&gt;
/// &lt;param name="data"&gt;Compressed ViewState to decompress&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static byte[] Decompress(byte[] data)
{
    //create a MemoryStream for holding the incoming data
    MemoryStream input = new MemoryStream();
    //write the incoming bytes to the MemoryStream
    input.Write(data, 0, data.Length);
    //set our position to the start of the Stream
    input.Position = 0;
    //create an instance of the GZipStream to decompress
    //the incoming byte array (the compressed ViewState)
    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
    //create a new MemoryStream for holding
    //the output
    MemoryStream output = new MemoryStream();
    //create a byte array
    byte[] buff = new byte[64];
    int read = -1;
    //read the decompressed ViewState into
    //our byte array, set that value to our
    //read variable (int data type)
    read = gzip.Read(buff, 0, buff.Length);
    //make sure we have something to read
    while (read &gt; 0)
    {
        //write the decompressed bytes to our
        //out going MemoryStream
        output.Write(buff, 0, read);
        //get the rest of the buffer
        read = gzip.Read(buff, 0, buff.Length);
    }
    gzip.Close();
    //return our out going MemoryStream
    //in an array
    return output.ToArray();
}
}</pre>
<p><span style="font-family:Georgia;font-size:small;"><br />
</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> protected</span><span> <span style="color:blue;">override</span> <span style="color:blue;">object</span> LoadPageStateFromPersistenceMedium() </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">string</span> viewState = (<span style="color:blue;">string</span>)Session[Session.SessionID]; </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">byte</span>[] bytes = <span style="color:#2b91af;">Convert</span>.FromBase64String(viewState); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> bytes = Compressor.Decompress(bytes); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> LosFormatter formatter = <span style="color:blue;">new</span> LosFormatter(); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">return</span> formatter.Deserialize(<span style="color:#2b91af;">Convert</span>.ToBase64String(bytes)); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> SavePageStateToPersistenceMedium(<span style="color:blue;">object</span> state) </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> LosFormatter formatter = <span style="color:blue;">new</span> LosFormatter(); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:#2b91af;">StringWriter</span> writer = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringWriter</span>(); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> formatter.Serialize(writer, state); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">string</span> viewStateString = writer.ToString(); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">byte</span>[] bytes = <span style="color:#2b91af;">Convert</span>.FromBase64String(viewStateString); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> bytes = Compressor.Compress(bytes); </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> Session[Session.SessionID] = <span style="color:#2b91af;">Convert</span>.ToBase64String(bytes);</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> }</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>5. <span style="font-family:'Cambria Math';font-size:small;"><strong>ASP.NET 2.0 approach for saving viewstate on server:</strong></span></p>
<p><span style="font-family:Georgia;font-size:small;">In ASP.NET 2.0, ViewState is saved by a descendant of </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.pagestatepersister.aspx" target="_blank"><em><span style="font-family:Georgia;font-size:small;">PageStatePersister</span></em></a><span style="font-family:Georgia;font-size:small;"> class. This class is an abstract class for saving and loading ViewsState and there are two implemented descendants of this class in .Net Framework, named </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.hiddenfieldpagestatepersister.aspx" target="_blank"><em><span style="font-family:Georgia;font-size:small;">HiddenFieldPageStatePersister</span></em></a><span style="font-family:Georgia;font-size:small;"> and </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.sessionpagestatepersister.aspx" target="_blank"><em><span style="font-family:Georgia;font-size:small;">SessionPageStatePersister</span></em></a><span style="font-family:Georgia;font-size:small;">. By default <em>HiddenFieldPageStatePersister</em> is used to save/load ViewState information, but we can easily get the <em>SessionPageStatePersister</em> to work and save ViewState in Session object. The only thing to do this is to override </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.pagestatepersister.aspx" target="_blank"><em><span style="font-family:Georgia;font-size:small;">PageStatePersister</span></em></a><span style="font-family:Georgia;font-size:small;"> property of <em>Page</em> class and ask it to return an instance of <em>SessionPageStatePersister</em> class:</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span>protected</span><span> <span style="color:blue;">override</span> <span style="color:teal;">PageStatePersister</span> PageStatePersister </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span>{ </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">get </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">return</span> <span style="color:blue;">new</span> <span style="color:teal;">SessionPageStatePersister</span>(Page); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="line-height:115%;">}</span><span style="line-height:115%;font-size:12pt;"> </span></p>
<p>&nbsp;</p>
<p><span style="font-family:Georgia;font-size:small;">The <em>PageStatePersister</em> class can be inherited to also created custom storage mediums for session state. The <em>PageStatePersister</em> class has two methods <em>Load()</em> and <em>Save()</em> which can be used to provide custom loading and saving media for ViewState.</span></p>
<p style="text-align:justify;line-height:18pt;margin:.2in 0;"><span>public</span><span> <span style="color:blue;">class</span> <span style="color:teal;">StreamPageStatePersister</span> : <span style="color:teal;">PageStatePersister </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> <span style="color:blue;">public</span> StreamPageStatePersister(<span style="color:teal;">Page</span> page) </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> : <span style="color:blue;">base</span>(page) </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> public</span><span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> Load() </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:teal;">Stream</span> stateStream = GetSecureStream(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p><span> <span style="color:green;">// Read the state string, using the StateFormatter. </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:teal;">StreamReader</span> reader = <span style="color:blue;">new</span> <span style="color:teal;">StreamReader</span>(stateStream); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p><span> <span style="color:teal;">IStateFormatter</span> formatter = <span style="color:blue;">this</span>.StateFormatter; </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">string</span> fileContents = reader.ReadToEnd(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p><span> <span style="color:green;">// Deserilize returns the Pair object that is serialized in </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:green;">// the Save method. </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:teal;">Pair</span> statePair = (<span style="color:teal;">Pair</span>)formatter.Deserialize(fileContents); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p><span> ViewState = statePair.First; </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> ControlState = statePair.Second; </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> reader.Close(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> stateStream.Close(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:green;"> </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> Save() </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> <span style="color:blue;">if</span> (ViewState != <span style="color:blue;">null</span> || ControlState != <span style="color:blue;">null</span>) </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">if</span> (Page.Session != <span style="color:blue;">null</span>) </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> { </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:teal;">Stream</span> stateStream = GetSecureStream(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> <span style="color:teal;">StreamWriter</span> writer = <span style="color:blue;">new</span> <span style="color:teal;">StreamWriter</span>(stateStream); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> <span style="color:teal;">IStateFormatter</span> formatter = <span style="color:blue;">this</span>.StateFormatter; </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:teal;">Pair</span> statePair = <span style="color:blue;">new</span> <span style="color:teal;">Pair</span>(ViewState, ControlState); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> <span style="color:green;">// Serialize the statePair object to a string. </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">string</span> serializedState = formatter.Serialize(statePair); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span></p>
<p>&nbsp;</p>
<p><span> writer.Write(serializedState); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> writer.Close(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> stateStream.Close(); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">else </span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> <span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:teal;">InvalidOperationException</span>(&#8220;Session needed for StreamPageStatePersister.&#8221;); </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> } </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="line-height:115%;"> }</span><span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family:Georgia;font-size:small;">Then a </span><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.adapters.pageadapter.aspx" target="_blank"><span style="font-family:Georgia;font-size:small;">PageAdapter</span></a><span style="font-family:Georgia;font-size:small;"> can be used to use this persister</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span>public</span><span> </span><span>class</span><span> MyPageAdapter : System.Web.UI.Adapters.PageAdapter </span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span>{</span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span><span>public</span><span> override PageStatePersister GetStatePersister() {</span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> </span><span>return</span><span> </span><span>new</span><span> Samples.AspNet.CS.StreamPageStatePersister(Page);</span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span> }</span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span>}</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family:Georgia;font-size:small;">This adapter can be configured via a <strong><em>.browser</em></strong> file placed in <strong><em>App_Browsers</em></strong> folder locally with the web site directory.</span></p>
<div style="line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">browsers</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">browser</span> <span style="color:#ff0000;">refid</span><span style="color:#0000ff;">="Default"</span> <span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;">        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">controlAdapters</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">            <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">adapter</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;">                <span style="color:#ff0000;">controlType</span><span style="color:#0000ff;">="System.Web.UI.Page"</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">                <span style="color:#ff0000;">adapterType</span><span style="color:#0000ff;">="MyPageAdapter"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;">        <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">controlAdapters</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">browser</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">browsers</span><span style="color:#0000ff;">&gt;</span></pre>
<p>&nbsp;</p>
</div>
</div>
<p><span style="font-family:'Cambria Math';font-size:small;"> </span></p>
<p><span style="font-family:Georgia;font-size:small;">This all above content was related to removing/reducing the Viewstate getting transferred over network. But there is still one more type of state “<strong><em>ControlState</em></strong>”. In layman’s terms, Its the minimal information that a control needs to render and function itself properly after a postback. A more apt definition from </span><a href="http://msdn.microsoft.com/en-us/library/1whwt1k7.aspx" target="_blank"><span style="font-family:Georgia;font-size:small;">MSDN</span></a><span style="font-family:Georgia;font-size:small;"> is:</span></p>
<blockquote><p><span style="font-family:Calibri;font-size:small;">Control state, introduced in ASP.NET version 2.0, is similar to view state but functionally independent of view state. A page developer can disable view state for the page or for an individual control for performance. However, control state cannot be disabled. Control state is designed for storing a control&#8217;s essential data (such as a pager control&#8217;s page number) that must be available on postback to enable the control to function even when view state has been disabled. By default, the ASP.NET page framework stores control state in the page in the same hidden element in which it stores view state. Even if view state is disabled, or when state is managed using Session, control state travels to the client and back to the server in the page. On postback, ASP.NET deserializes the contents of the hidden element and loads control state into each control that is registered for control state.</span></p></blockquote>
<p><span style="font-family:Georgia;font-size:small;">As it is mentioned in the above description, that ControlState will be traveling with the page all the time. There may come a requirement where we want to have it stored somewhere else other then the page hidden fields. While searching for methods to reduce the viewstate size, i stumbled across </span><a href="http://szokelizer.blogsome.com/2006/11/09/how-to-put-controlstate-into-viewstate-and-how-to-put-viewstate-into-session/" target="_blank"><span style="font-family:Georgia;font-size:small;">this</span></a><span style="font-family:Georgia;font-size:small;"> post by <em>SZOKELIZER</em>, which describes a way to store the ControlState in session as well. ASP.NET provides a switch “</span><a href="http://msdn.microsoft.com/en-us/library/system.web.configuration.httpcapabilitiesbase.requirescontrolstateinsession.aspx" target="_blank"><span style="font-family:Georgia;font-size:small;">RequiresControlStateInSession</span></a><strong><em> </em></strong><span style="font-family:Georgia;font-size:small;">” which allows to store the control state in session rather then the page itself. So for the sake of completeness I am putting the code that needs to be put to enable storing the control state in session from the above mentioned post.</span></p>
<div style="line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="line-height:12pt;background-color:white;width:100%;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">system.web</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">browserCaps</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;">      <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">case</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">        RequiresControlStateInSession=true</pre>
<pre style="line-height:12pt;background-color:white;width:100%;">      <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">case</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;">    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">browserCaps</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">system.web</span><span style="color:#0000ff;">&gt;</span></pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<h4>Conclusion</h4>
<p><span style="font-family:Georgia;font-size:small;">In this post I have written about different mechanisms to reduce size of viewstate that travels along with an asp/aspx page every time. I have tried to covered few of the approaches that can be used but I am sure there can be many more. One such approach suggested by a friend of mine is using <strong><em>HttpHandlers</em></strong> to cut the viewstate from the request and restore it back. I am working on a poc for this. I will post it once it is complete. Please do post a comment, if you have come across any other way to tackle this or if there is some correction in approaches I have described above.</span></p>
<br />Posted in .Net 2.0, ASP.Net, C#, ViewState Tagged: .browser, ControlState, IIS compression, LoadPageStateFromPersistenceMedium, PageAdapter, PageStatePersister, RequiresControlStateInSession, SavePageStateToPersistenceMedium, Session, ViewState, Viewstate compression <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=161&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/09/16/viewstate-compression-in-asp-net-2-0/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>Request.ValidateInput() : A common misconception</title>
		<link>http://gargmanoj.wordpress.com/2009/09/07/request-validateinput-a-common-misconception/</link>
		<comments>http://gargmanoj.wordpress.com/2009/09/07/request-validateinput-a-common-misconception/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 12:17:00 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[A potentially dangerous Request.Form value was detected from the client]]></category>
		<category><![CDATA[HttpRequestValidationException]]></category>
		<category><![CDATA[Request.ValidateInput()]]></category>
		<category><![CDATA[ValidateRequest]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/09/07/request-validateinput-a-common-misconception/</guid>
		<description><![CDATA[Attackers try to exploit smallest possible hole into the security of a website. A common and widely used method is script injection attack or cross site scripting where attacker tries to put some script as input to the website and tries to damage the system. A simplest example could be passing a java script function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=172&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p align="justify"><font size="3" face="Cambria">Attackers try to exploit smallest possible hole into the security of a website. A common and widely used method is script injection attack or cross site scripting where attacker tries to put some script as input to the website and tries to damage the system. A simplest example could be passing a java script function that redirects every request to a page to some other site next time onwards or passing a SQL DML statement which deletes/updates record into the database. </font></p>
<p align="justify"><font size="3" face="Cambria">ASP.Net provide some measure to mitigate this risk of script injection. One of such technique is validating each and every input entered by the user before passing it on the server automatically. In this approach IIS parses the input before it is sent to the worker process and checks it for potential dangerous characters lie &lt;, &gt; etc. If any of such character is found in the input request, an exception is thrown, thus saving the site from a possible attack. </font></p>
<p align="justify"><font size="3" face="Cambria">The exception thrown is </font></p>
<blockquote><p align="center"><font size="3" face="Cambria">“A potentially dangerous Request.Form value was detected from the client”</font></p>
</blockquote>
<p align="justify"><font size="3" face="Cambria">In ASP.NET this feature can be applied per page basis or for the entire site depending on the requirement. To enable input request validation for the entire site, set <em><font color="#0000ff">validateRequest</font></em> attribute of <em><font color="#0000ff">page</font></em> section in <em><font color="#0000ff">web.config</font></em> to <em><font color="#0000ff">true</font></em>.</font></p>
<p align="justify"><font size="3" face="Cambria"></font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">system.web</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">pages</span> <span style="color:#ff0000;">validateRequest</span><span style="color:#0000ff;">=&quot;false&quot;</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">system.web</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">configuration</span><span style="color:#0000ff;">&gt;</span></pre>
</p></div>
</div>
<p align="justify"><font size="3" face="Cambria">Similarly, to override the request validation or enable/disable request validation on a page <font color="#0000ff">ValidateRequest</font> attribute of <font color="#0000ff"><a href="http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx" target="_blank">Page</a> </font>directive can be set to <font color="#0000ff">true/false</font> respectively. Setting this attribute to true instructs the IIS to check the sanity of the incoming request before passing it on.</font></p>
<p align="justify"><font size="3" face="Cambria">So this approach won’t let any script to pass through. But there are situations where you need to take a script or the prohibited characters like &lt;,&gt; as input from user, at the same time preventing a misuse of this opportunity of script injection. </font></p>
<p align="justify"><font size="3" face="Cambria">A commonly used approach in ASP.NET for this scenario is:</font></p>
<ol>
<li>
<div align="justify"><font size="3" face="Cambria">Set <font color="#0000ff">ValidateRequest</font> to false for that page. </font></div>
</li>
<li>
<div align="justify"><font size="3" face="Cambria">Encode the input entered by the end user using <font size="3" face="Cambria"><a href="http://msdn.microsoft.com/en-us/library/ms525347.aspx" target="_blank">Server.HtmlEncode()</a>:</font> Encoding ensures that all the stop words(potentially harmful characters) are converted to an equivalent HTML character encoding e.g. “<strong><em>&lt;</em></strong>” is converted to “<strong><em>&amp;lt;</em></strong>” etc. </font></div>
</li>
</ol>
<p align="justify"><font size="3" face="Cambria">I had a similar requirement, few days back, when in our application on “contact us” page, We wanted to allow user to enter html scripts into a text box. So we disabled the <font color="#0000ff">ValidateRequest</font> attribute to false and used <font size="3" face="Cambria"><a href="http://msdn.microsoft.com/en-us/library/ms525347.aspx" target="_blank">Server.HtmlEncode()</a>&#160;</font>method to encode the entered input. But at the same time we didn’t wanted the user to pass something malicious through the query string or the cookies. ASP.NET also provides a method on the <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequest(VS.80).aspx" target="_blank">Request</a> object named <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequest.validateinput(VS.80).aspx" target="_blank">ValidateInput()</a>, which validates the request input passed by the client to the server. And if something fishy is found in the request an <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestvalidationexception(VS.80).aspx">HttpRequestValidationException</a> exception is thrown. And this is where the confusion gets in.</font></p>
<p align="justify"><font size="3" face="Cambria">As per <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequest.validateinput(VS.85).aspx" target="_blank">MSDN</a>, Request.ValidateInput() method ….</font></p>
<blockquote>
<p align="justify"><font size="3" face="Candara">The </font><a href="http://msdn.microsoft.com/en-us/library/system.web.httprequest(VS.85).aspx"><font size="3" face="Candara">HttpRequest</font></a><font size="3" face="Candara"> class uses input validation flags to track whether to perform validation on the request collections accessed through the <b>Cookies</b>, <b>Form</b>, and <b>QueryString </b>properties. The <b>ValidateInput</b> method sets these flags so that when the get accessors for the <b>Cookies</b>, <b>Form</b>, or <b>QueryString</b> property are invoked, input validation is performed. Validation works by checking all input data against a hard-coded list of potentially dangerous data.</font></p>
<p align="justify"><font size="3" face="Candara">If the validation feature is enabled by page directive or configuration, this method is called during the page&#8217;s <b>ProcessRequest</b> processing phase. The <b>ValidateInput</b> method can be called by your code if the validation feature is not enabled.</font></p>
</blockquote>
<p align="justify"><font size="3" face="Cambria">As this definition says that ValidateInput method does nothing but sets some flags indicating that all access to user input must be validated before using it. But normally developer tend to call Request.ValidateInput() method and then expect it to throw the <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestvalidationexception(VS.80).aspx">HttpRequestValidationException</a> exception, which will not happen since this method doesn’t throw any exception, rather any access now onwards to input will throw the above mentioned exception.</font></p>
<p align="justify"><font size="3" face="Cambria">Lets disassemble the Request.ValidateInput method.</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> ValidateInput()</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">{</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>._flags.Set(1);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>._flags.Set(2);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>._flags.Set(4);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>._flags.Set(0x40);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>._flags.Set(0x80);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p align="justify"><font size="3" face="Cambria">As shown in the above code snippet, this method has just some set methods calls. Actually these method set a flag for one of the input approach that can be used by the client. Like querystring, form input , cookie etc. </font></p>
<p align="justify"><font size="3" face="Cambria"><em>So what does this setter method do?? </em>This method is merely an indicator for the getters methods of each of the request fields to check the sanitation of the value before returning the value to the requestor and if there is some invalid input then throw the <a href="http://msdn.microsoft.com/en-us/library/system.web.httprequestvalidationexception(VS.80).aspx">HttpRequestValidationException</a>.</font></p>
<p align="justify"><font size="3" face="Cambria">So to check for invalid input in Form values, we need to access form values collection of the request as Request.Form after the call to Request.ValidateInput(). Following is a sample code for validating Form values, </font></p>
<pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;background-color:#fbfbfb;min-height:40px;width:712px;height:227px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding:5px;"><pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">String firstName = <span style="color:#0000ff;">string</span>.Empty;
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;"><span style="color:#0000ff;">try</span>
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">{
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">    Request.ValidateInput();
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">    <span style="color:#008000;">//Access the Form value collection. So if there is any non complient input entered by user.. It will throw an exception</span>
</pre>
<pre style="background-color:#ffff00;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">    System.Collections.Specialized.NameValueCollection nvc = Request.Form;
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;"></pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">    firstName = txtName.Text.Trim();
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">}
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;"><span style="color:#0000ff;">catch</span>(HttpRequestValidationException)
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">{
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">    firstName = HttpUtility.HtmlEncode(txtName.Text.Trim());
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:14px;margin:0;">}</pre>
<p align="justify"><font size="3" face="Cambria Math">here line in yellow color will throw an exception if at all there is some bad data in the input. Then in catch we can use HtmlEncode() to encode the user input.</font></p>
<p align="justify"><font size="3" face="Cambria Math">Similar approach can be used while accessing cookies or QueryString.</font></p>
<br />Posted in .Net 2.0, ASP.Net, C# Tagged: A potentially dangerous Request.Form value was detected from the client, HttpRequestValidationException, Request.ValidateInput(), ValidateRequest <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=172&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/09/07/request-validateinput-a-common-misconception/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>Fixing &#8220;This page contains secure and nonsecure items&#8221; warning in IE</title>
		<link>http://gargmanoj.wordpress.com/2009/07/17/fixing-this-page-contains-secure-and-nonsecure-items-warning-in-ie/</link>
		<comments>http://gargmanoj.wordpress.com/2009/07/17/fixing-this-page-contains-secure-and-nonsecure-items-warning-in-ie/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 12:11:00 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[IE]]></category>
		<category><![CDATA[Security warning]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/07/17/fixing-this-page-contains-secure-and-nonsecure-items-warning-in-ie/</guid>
		<description><![CDATA[Ever encountered “This page contains both secure and nonsecure items. Do you want to display the nonsecure items? ” in IE. Well I do and this pesky popup keeps bugging me now n then. Till today, I never bothered about it. I used to click “Yes” no matter what . I know it’s not always [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=127&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<blockquote><p align="justify"><font size="2">Ever encountered “<font color="#0000ff" size="3" face="Georgia">This page contains both secure and nonsecure items. Do you want to display the nonsecure items?</font> ” in IE. Well I do and this pesky popup keeps bugging me now n then. Till today, I never bothered about it. I used to click “Yes” no matter what <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . I know it’s not always safe to do it but who cares <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </font></p>
</blockquote>
<p align="justify"><font size="2"><img style="border-bottom:0;border-left:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;" title="CapturedImage_13" border="0" alt="CapturedImage_13" src="http://gargmanoj.files.wordpress.com/2009/07/capturedimage_131.jpg?w=365&#038;h=180" width="365" height="180" /> </font></p>
<p align="justify"><font size="2"><font size="5">T</font>oday I was assigned a bug, in the application I am working on, which says that users are seeing this popup so many times and they are not happy about it. That got me thinking people do care about these pop ups as well <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . </font></p>
<p align="justify"><font size="2">Ne way, I had to find a solution to get rid of this warning. So after little <a href="http://bit.ly/LLtgp" target="_blank">Binging and Googling</a> I found the solution. So thought about writing my findings though they are available in top 10 search results <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </font></p>
<p align="justify"><font size="2">The reason for this warning is that you are visiting a secure site (HTTPS site) and somewhere behind the scenes that site or page is trying to access a non secure resource be it a complete page or some image. That makes the browser assume that since you are inside a secure site, you should not be getting anything that’s not secure. Like many a times https pages have some images whose source is http for example </font><a href="http://mmyserver/myimages"><font size="2">http://mmyserver/myimages</font></a><font size="2"> .</font></p>
<p align="justify"><font size="2">There are two ways you can get rid of this warning</font></p>
<ol>
<li>
<div align="justify"><font size="2">Change all reference of <strong>http</strong> to <strong>https</strong> in the page. </font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>Lower your guard in IE</strong>: IE has a setting which decide what the browser behavior in case of a non secure content coming while browsing a secure page. This setting is in “<strong>IE</strong> ==&gt; <strong>Tools</strong> ==&gt; <strong>Internet Options</strong> ==&gt; <strong>Security Tab</strong> ==&gt; Click<strong> Custom Level</strong> button” this will open up a list of options for various security settings for IE. Scroll this list to search for “<strong>Display Mixed Content</strong>” option. The “<strong>Prompt</strong>” radio option is selected by default. Change it to “<strong>Enable</strong>” option and click OK. and you are done. The change will take effect immediately. </font></div>
</li>
</ol>
<p align="justify"><font size="2">Following are some of the links which helped me to solved me the issue:</font></p>
<p>1. <a href="http://ask-leo.com/can_i_get_rid_of_the_this_page_contains_both_secure_and_nonsecure_items_warning.html">http://ask-leo.com/can_i_get_rid_of_the_this_page_contains_both_secure_and_nonsecure_items_warning.html</a></p>
<p>2. <a href="http://www.sslshopper.com/article-stop-the-page-contains-secure-and-nonsecure-items-warning.html?jn948e2cf0=2#jotnav948e2cf0db15fcce0a996d225ead01c0">http://www.sslshopper.com/article-stop-the-page-contains-secure-and-nonsecure-items-warning.html?jn948e2cf0=2#jotnav948e2cf0db15fcce0a996d225ead01c0</a></p>
<p>3. <a href="http://www.velocityreviews.com/forums/t152692-this-page-contains-both-secure-and-nonsecure-items.html">http://www.velocityreviews.com/forums/t152692-this-page-contains-both-secure-and-nonsecure-items.html</a></p>
<p>4. <a href="http://blogs.msdn.com/jorman/archive/2006/02/06/526087.aspx">http://blogs.msdn.com/jorman/archive/2006/02/06/526087.aspx</a></p>
<p>5. <a href="http://www.wallpaperama.com/forums/windows-disable-this-page-contains-both-secure-and-nonsecure-items-t274.html">http://www.wallpaperama.com/forums/windows-disable-this-page-contains-both-secure-and-nonsecure-items-t274.html</a></p>
<p>6. <a href="http://www.aspdeveloper.net/tiki-index.php?page=HTMLTipsSSLNonSecureWarning">http://www.aspdeveloper.net/tiki-index.php?page=HTMLTipsSSLNonSecureWarning</a></p>
<br />Posted in IE Tagged: IE, Security warning <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=127&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/07/17/fixing-this-page-contains-secure-and-nonsecure-items-warning-in-ie/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>

		<media:content url="http://gargmanoj.files.wordpress.com/2009/07/capturedimage_131.jpg" medium="image">
			<media:title type="html">CapturedImage_13</media:title>
		</media:content>
	</item>
		<item>
		<title>Pagination with JavaScript</title>
		<link>http://gargmanoj.wordpress.com/2009/07/17/pagination-with-javascript/</link>
		<comments>http://gargmanoj.wordpress.com/2009/07/17/pagination-with-javascript/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 08:34:00 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[js pagination javascript]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/07/17/pagination-with-javascript/</guid>
		<description><![CDATA[Pagination is a must have feature when it comes to display a list of item. List could be collection of search results, a tabular display of names etc etc. When the list is small, pagination is not something we should be worried about, But imagine having a list with 100’s of items to show then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=120&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p align="justify"><font size="2">Pagination is a must have feature when it comes to display a list of item. List could be collection of search results, a tabular display of names etc etc. When the list is small, pagination is not something we should be worried about, But imagine having a list with 100’s of items to show then displaying all of them at once on a page is not a good idea. </font></p>
<ul>
<li>
<div align="justify"><font size="2">Viewer of that page have to scroll down a lot if the list if too long</font></div>
</li>
<li>
<div align="justify"><font size="2">Page becomes slower since there are lots of DOM elements on the page</font></div>
</li>
</ul>
<p align="justify"><font size="2">So developers tend to display a small number of items at once to the viewer and then proving him/her the option of navigating to different pages. There are two ways once can implement paging </font></p>
<ol>
<li>
<div align="justify"><font size="2"><strong>Server side paging</strong>: Developer can get only a fixed number of records from the server and display then to the viewer and whenever viewer wants to navigate to a different page, a new request demanding the items from that particular page can be send to the server. In turn server can return records from that page only.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>Client side paging</strong>: Depending on the scenario developer can also do the job of showing pages on the client side as well i.e. he/she may request all the items at once and then showing only a set of them to the viewer and providing user with the links to other pages using any scripting language. </font></div>
</li>
</ol>
<p align="justify"><font size="2">I had a situation where it was not possible to have server side pagination due to restriction of the framework I was coding against. So I had no choice but to go with client side paging. I was implementing a search functionality where I would be searching against the lucene index to get the results and then showing those results to the viewer. search results will be displayed a html table. each row in the result table will be used to display one search result. Each search result will have some predefined fields and a summary for that results. Viewer will have an option to show and hide the summary of the result. So one requirement was to have different number of items per page depending on whether summary is visible to the viewer or not.</font></p>
<p align="justify"><font size="2">I took the following approach:</font></p>
<p align="justify"><font size="2">After getting results from server, I used JavaScript to create DOM elements for each of the search results returned. After all the results are created on the page, pagination code was called to show the first page and the pager.</font></p>
<p align="justify"><font size="2">To start I created a JS class named “Pager” which will have all the methods related to the navigation among pages. Pager class had following members:</font></p>
<ul>
<li>
<div align="justify"><font size="2"><strong>tableName</strong> : Name of the table on which pagination has to be applied.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>itemsPerPage</strong> : Number of items to show when summary field is visible.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>itemsWithoutSmry</strong> : Number of items to show when summary field is not visible to the viewer.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>currentItemPerPage</strong> : Number of items currently visible. This value will be equal to either <em>itemsPerPage</em> or <em>itemsWithoutSmry</em>.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>currentPage</strong> : current page in the table.</font></div>
</li>
<li>
<div align="justify"><font size="2"><strong>pages</strong> : Total number of pages in the table.</font></div>
</li>
</ul>
<p><font size="2">All the above field will be initialized to some default values when the Object of Pager call will be created. </font></p>
<h6><font size="2">Initialization of the pager</font></h6>
<p><font size="2">This method will initialize the pager object field <strong>pages</strong> by using the total records and items to shown on page. Following code snippet is the init() method.</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:1pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//initialize the pager</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">  <span style="color:#0000ff;">this</span>.init = <span style="color:#0000ff;">function</span>() {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">//check if table exists in the dom</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span>(document.getElementById(tableName) != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                <span style="color:#008000;">// get the total number of rows in the table</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">var</span> rows = document.getElementById(tableName).rows;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">var</span> records = (rows.length - 1);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                <span style="color:#008000;">// Calculate the number of pages</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">this</span>.pages = Math.ceil(records / <span style="color:#0000ff;">this</span>.itemsPerPage);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                <span style="color:#008000;">// set the flag to indicate initialization is complete</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">this</span>.inited = <span style="color:#0000ff;">true</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">   }</pre>
</p></div>
</div>
<h6><font size="2">Showing records of a particular page</font></h6>
<p><font size="2">To do this I will iterate through the rows of the table to show/hide the visibility of these rows depending on the page viewer has requested. Following code snippet shows the methods that do this.</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//Show record from a start index to an end index</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">this</span>.showRecords = <span style="color:#0000ff;">function</span>(from, to) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">// check if table exists in the DOM</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span>(document.getElementById(tableName) != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">//Get rows in the table and then set the visibility of the rows between 'from' and 'to'</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">var</span> rows = document.getElementById(tableName).rows;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#008000;">// i starts from 1 to skip table header row and hide rows other then the current page</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">var</span> i = 1; i &lt; rows.length; i++) {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                <span style="color:#0000ff;">if</span> (i &lt; from || i &gt; to)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                    rows[i].style.display = <span style="color:#006080;">'none'</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">                    rows[i].style.display = <span style="color:#006080;">''</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">// Method to show a particular pager</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"> <span style="color:#0000ff;">this</span>.showPage = <span style="color:#0000ff;">function</span>(pageNumber) {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">// check if initialization is done</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span> (! <span style="color:#0000ff;">this</span>.inited) {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            alert(<span style="color:#006080;">&quot;not inited&quot;</span>);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">return</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">//change the CSS style of the old page</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//each page number indicator in the pager has a naming convention like tablename+pg+pageindex</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> oldPageAnchor = document.getElementById(<span style="color:#0000ff;">this</span>.tableName+<span style="color:#006080;">'pg'</span>+<span style="color:#0000ff;">this</span>.currentPage);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span>(oldPageAnchor)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        oldPageAnchor.className = <span style="color:#006080;">'pg-normal'</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">//change the style of the current page</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.currentPage = pageNumber;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> newPageAnchor = document.getElementById(<span style="color:#0000ff;">this</span>.tableName+<span style="color:#006080;">'pg'</span>+<span style="color:#0000ff;">this</span>.currentPage);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span>(newPageAnchor)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        newPageAnchor.className = <span style="color:#006080;">'pg-selected'</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">       <span style="color:#008000;">//calculate current page records index</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> from = (pageNumber - 1) * <span style="color:#0000ff;">this</span>.currentItemPerPage + 1;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> to = from + <span style="color:#0000ff;">this</span>.currentItemPerPage - 1;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//show the records</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.showRecords(from, to);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }   </pre>
</p></div>
</div>
<p>&#160;</p>
<h6><font size="2">Navigating to Next and Previous pages</font></h6>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//show previous page</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.prev = <span style="color:#0000ff;">function</span>() {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>.currentPage &gt; 1)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">this</span>.showPage(<span style="color:#0000ff;">this</span>.currentPage - 1);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#008000;">//show next page</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.next = <span style="color:#0000ff;">function</span>() {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>.currentPage &lt; <span style="color:#0000ff;">this</span>.pages) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">this</span>.showPage(<span style="color:#0000ff;">this</span>.currentPage + 1);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }</pre>
</p></div>
</div>
<h6><font size="2">Showing the pager bar</font></h6>
<p><font size="2">Following code snippet contains javascript method which will create a div at the end of table containing number from 1 to the total number of pages in the table along with prev and next page links. each link will have a onclick javascript function call to the above mentioned functions.</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//show the pager bar</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">// param : @pagerName: name of the pager object</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//  param : @positionId: Id of the div in which the page has to be shown </span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.showPageNav = <span style="color:#0000ff;">function</span>(pagerName, positionId) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#008000;">// Check if the pager object has been initialized</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#0000ff;">if</span> (! <span style="color:#0000ff;">this</span>.inited) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        alert(<span style="color:#006080;">&quot;not inited&quot;</span>);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#008000;">// check if the div exisits in DOM </span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#0000ff;">if</span>(document.getElementById(positionId) != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> element = document.getElementById(positionId);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">// create prev page link with call to prev() method</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">var</span> pagerHtml = <span style="color:#006080;">'&lt;b&gt;&lt;span onclick=&quot;'</span> + pagerName + <span style="color:#006080;">'.prev();&quot; class=&quot;pg-normal&quot;&gt; &amp;#171 Prev &lt;/span&gt; | '</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">// create numeric page number links</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">var</span> page = 1; page &lt;= <span style="color:#0000ff;">this</span>.pages; page++)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">              pagerHtml += <span style="color:#006080;">'&lt;span id=&quot;'</span>+<span style="color:#0000ff;">this</span>.tableName+<span style="color:#006080;">'pg'</span> + page + <span style="color:#006080;">'&quot; class=&quot;pg-normal&quot; onclick=&quot;'</span> + pagerName + <span style="color:#006080;">'.showPage('</span> + page + <span style="color:#006080;">');&quot;&gt;'</span> + page + <span style="color:#006080;">'&lt;/span&gt; | '</span>;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">// Create next page link with call to next() method</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        pagerHtml += <span style="color:#006080;">'&lt;span onclick=&quot;'</span>+pagerName+<span style="color:#006080;">'.next();&quot; class=&quot;pg-normal&quot;&gt; Next »&lt;/span&gt; &lt;/b&gt;'</span>;</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#008000;">// chnage the inner html of the pager Div </span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        element.innerHTML = pagerHtml;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }</pre>
</p></div>
</div>
<h6><font size="2">Adjusting the record count per page with the Summary visibility</font></h6>
<p><font size="2">I added a method to the pager class which takes a Boolean parameter denoting the visibility status of the summary content as input and based on this parameter it changes the current page size. Following code snippet shows the same:</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//adjust page according to summay div visibility</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">// param : @isSummaryShown = Visibility status of the summary content for the item</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.adjustPager = <span style="color:#0000ff;">function</span>(isSummaryShown)</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#008000;">//change the pagesize</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#0000ff;">if</span>(isSummaryShown)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.currentItemPerPage = <span style="color:#0000ff;">this</span>.itemsPerPage;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#0000ff;">else</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.currentItemPerPage = <span style="color:#0000ff;">this</span>.itemsWithoutSmry;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#008000;">//rebuild pager</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      <span style="color:#0000ff;">if</span>(document.getElementById(tableName) != <span style="color:#0000ff;">null</span>)</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">      {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">          <span style="color:#0000ff;">var</span> rows = document.getElementById(tableName).rows;</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">          <span style="color:#0000ff;">var</span> records = (rows.length - 1);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">          <span style="color:#0000ff;">this</span>.pages = Math.ceil(records / <span style="color:#0000ff;">this</span>.currentItemPerPage);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.showPageNav(<span style="color:#006080;">&quot;pagerObj&quot;</span>, <span style="color:#006080;">'pageNavPosition_Results'</span>);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">this</span>.showPage(1);</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">    }</pre>
</p></div>
</div>
<h6></h6>
<h6><font size="2">Creating and using Instance of the Pager Class</font></h6>
<p><font size="2">following line shows the signature for the pager class</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">function</span> Pager(tableName, itemsPerPage, pageSizeWithoutSummary)</pre>
</p></div>
</div>
<p><font size="2">after the page object is created it must be first initialized, then the pager bar should be created followed by showing the page.</font></p>
<p><font size="2">Following code snippet shows this</font></p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, &#39;max-height:200px;font-size:14pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">// create a pager obejct by passing tablename, page size</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">var</span> pagerObj = <span style="color:#0000ff;">new</span> Pager(<span style="color:#006080;">'table_AllResults'</span>, 10, 15);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">// init the pager object</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">pagerObj.init();</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//show the pager bar for itens without summary</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">pagerObj.adjustPager(<span style="color:#0000ff;">false</span>);</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#008000;">//show first page</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, &#39;color:black;font-size:14pt;overflow:visible;border-style:none;margin:0;padding:0;">pagerObj.showPage(1);</pre>
</p></div>
</div>
<pre><font size="2" face="var">I will be uploading a demo soon.</font></pre>
<p><font size="2" face="var">If you have some better way to implement the pagination then please drop a comment.&#160; <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </font></p>
<p>&#8211;</p>
<br />Posted in CSS, JavaScript Tagged: js pagination javascript <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/120/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=120&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/07/17/pagination-with-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
		<item>
		<title>Null-coalescing operator in C#</title>
		<link>http://gargmanoj.wordpress.com/2009/05/12/null-coalescing-operator-in-c/</link>
		<comments>http://gargmanoj.wordpress.com/2009/05/12/null-coalescing-operator-in-c/#comments</comments>
		<pubDate>Tue, 12 May 2009 10:02:00 +0000</pubDate>
		<dc:creator>Manoj Garg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[??]]></category>
		<category><![CDATA[null-coalescing operator]]></category>

		<guid isPermaLink="false">http://gargmanoj.wordpress.com/2009/05/12/null-coalescing-operator-in-c/</guid>
		<description><![CDATA[While surfing around I can across a term called “null-coalescing operator”. After searching about it, I came to know that C# has an operator ?? . well I didn’t new about it before so thought of writing about it. MSDN says The ?? operator is called the null-coalescing operator and is used to define a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=115&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>While surfing around I can across a term called “null-coalescing operator”. After searching about it, I <a href="http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx" target="_blank">came to know</a> that C# has an operator <strong>?? </strong>. well I didn’t new about it before so thought of writing about it. </p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms173224.aspx" target="_blank">MSDN</a> says</p>
<blockquote><p><em><font size="4" face="Latha">The <strong>??</strong> operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.</font></em></p>
</blockquote>
<p>Example:</p>
<pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;background-color:#fbfbfb;min-height:40px;width:650px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding:5px;"><pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:12px;margin:0;"><span style="color:#0000ff;">int</span>? x = <span style="color:#0000ff;">null</span>;
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:12px;margin:0;"><span style="color:#0000ff;">string</span> str = &quot;<span style="color:#8b0000;">Hello World !!</span>&quot;; 
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:12px;margin:0;"></pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:12px;margin:0;"><span style="color:#0000ff;">int</span> y = x ?? -99; <span style="color:#008000;">// Here Y will be -99 since X is null</span>
</pre>
<pre style="background-color:#fbfbfb;width:100%;font-family:consolas,&#39;font-size:12px;margin:0;"><span style="color:#0000ff;">string</span> newStr = str ?? &quot;<span style="color:#8b0000;">Default Hello World !!</span>&quot;; <span style="color:#008000;">// here newStr will be &quot;Hello World !!&quot;</span></pre>
<p>PS: This operator reminded me of similar functions available in SQL Server like ISNULL, COALESCE</p>
<p>--</p>
<br />Posted in C# Tagged: ??, null-coalescing operator <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gargmanoj.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gargmanoj.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gargmanoj.wordpress.com&#038;blog=1375338&#038;post=115&#038;subd=gargmanoj&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gargmanoj.wordpress.com/2009/05/12/null-coalescing-operator-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/573160ada16637cc5630116b72fd98de?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gargmanoj</media:title>
		</media:content>
	</item>
	</channel>
</rss>