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

<channel>
	<title>Alkampfer&#039;s Place&#187; ASP.NET</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to test SSL based WCF services</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/11/21/how-to-test-ssl-based-wcf-services/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/11/21/how-to-test-ssl-based-wcf-services/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 11:35:49 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[Wcf]]></category>
		<category><![CDATA[WCF IIS7]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/11/21/how-to-test-ssl-based-wcf-services/</guid>
		<description><![CDATA[I usually work with WCF service that needs to be secured with Certificates or simply using HTTPS. The usual question from other dev in the team is “how can I simulate HTTPS to work with WCF?” The problem arise that to test https sites, people usually work with IIS self issued certificates. Figure 1: Self-Signed [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F21%2Fhow-to-test-ssl-based-wcf-services%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F21%2Fhow-to-test-ssl-based-wcf-services%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=IIS,SSL,Wcf,WCF+IIS7&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I usually work with WCF service that needs to be secured with Certificates or simply using HTTPS. The usual question from other dev in the team is “how can I simulate HTTPS to work with WCF?”</p>
<p>The problem arise that to <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> https sites, people usually work with IIS self issued certificates.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLa82f3d.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="SNAGHTMLa82f3d" border="0" alt="SNAGHTMLa82f3d" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLa82f3d_thumb.png" width="529" height="233" /></a></p>
<p><strong>Figure 1: </strong><em>Self-Signed certificate in IIS</em></p>
<p>This works great for sites, you can use the auto signed certificate in your sites in https binding, then when you navigate to the site you usually got an error because the certificate is not issued for the right site. What I need is usually to modifiy the hosts file in windows, creating an alias of <a target="_blank" href="http://www.mydomain.com">www.mydomain.com</a> to 127.0.0.1, so I can directly point to the right address with WCF client application and can simply manage to use the local or remote service simply modifying the hosts file.</p>
<p>Sadly enough, WCF does not tolerate problem in certificates and this makes useless working with Self-Signed Certificate. To be able to use a WCF Service secured with SSL in your dev machine you should issue yourself a valid certificate. The solution is using the SelfSSL.exe tool that comes with the <a target="_blank" href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=17275">IIS6 Resource Kit Tools</a>.</p>
<p>Once installed you can simply go to installation folder with Administrator Command Prompt (you need to launch the command prompt as administrator or it wont work) and simply create a valid certificate with this command line   </p>
<p><strong><em>Selfssl /N:CN=www.codewrecks.com /V :2000 /S:3</em></strong></p>
<p>as you can see with the option /N:CN you are able to specify the Common Name you want to use, the /V option is used to specify the duraction in years of the certificate, and the /S: is used to specify the Id of the site you want to change, just select the “site” node on IIS7 administration console to see the ID assigned to each site.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image4.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image_thumb4.png" width="577" height="129" /></a></p>
<p><strong>Figure 2: </strong><em>How to find Site ID from IIS administration console</em></p>
<p>Now your site should have https binding enabled and it should use the autogenerated certificate, but if you browse the site your browser is still telling you that the certificate is not valid. If you open your certificate from the IIS administration console (just select the root node with your computer name and select <em>Server Certificates</em> from the IIS section on the right and double click on the desidered certificate) you can verify the error.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLb106f5.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="SNAGHTMLb106f5" border="0" alt="SNAGHTMLb106f5" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLb106f5_thumb.png" width="419" height="521" /></a></p>
<p><strong>Figure 3: </strong><em>The certificate is not valid because the certification authority is not trusted</em></p>
<p>The problem is that the Certification Authority of this certificate is not trusted, so you need to export this certificate to a file, just right-click the certificate and choose “Export”. (remember that you should choose a password to export the certificate)</p>
<p>Now open the mmc certification snap in (just Start-&gt;Run-&gt;<strong>mmc.exe certmgr.msc</strong>) and choose to import the certificate you just exported</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image5.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image_thumb5.png" width="384" height="239" /></a></p>
<p><strong>Figure 4: </strong><em>Import the certificate from the Certificates manager.</em></p>
<p>Choose the file you previously exported, insert the password you used for the export, then proceed to import the certificates. At the end of the operation, if you open again the certificate from the IIS administration console, everything should be ok.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image6.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image_thumb6.png" width="415" height="237" /></a></p>
<p><strong>Figure 5: </strong><em>The certificate is now ok and can be used to secure your sites</em></p>
<p>If this does not works, and you still got errors when you browse your local site through Https, probably the certificate was imported in the wrong path, just import the exported certificate again as shown in <strong>figure 4, </strong>but when you are asked for the location where you want to import the certificate press the button browse (<strong>Figure 6</strong>)</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image7.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/image_thumb7.png" width="497" height="259" /></a></p>
<p><strong>Figure 6: </strong><em>Import the certificate to a specified location.</em></p>
<p>Now choose the location as shown in <strong>Figure 7 </strong>and everything should work ok.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLbcf691.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="SNAGHTMLbcf691" border="0" alt="SNAGHTMLbcf691" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/11/SNAGHTMLbcf691_thumb.png" width="295" height="269" /></a></p>
<p><strong>Figure 7:</strong><em> Choose the exact location where to import the certificate.</em></p>
<p>Now modify the hosts file, mount your WCF service and verify with WCF <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> client that everything is ok.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/11/21/how-to-test-ssl-based-wcf-services/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The strangest bug of IE I&#8217;ve ever seen</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/09/07/the-strangest-bug-of-ie-ive-ever-seen/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/09/07/the-strangest-bug-of-ie-ive-ever-seen/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 15:57:51 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/09/07/the-strangest-bug-of-ie-ive-ever-seen/</guid>
		<description><![CDATA[We have a web application written in asp.net and we experienced really strange problem after recent modification. We have a strange behavior in IE9 with redirects and after some inspection we found a really strange fact (that almost make us lost 2 hours of work). To recreate this situation simply create a new page in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F09%2F07%2Fthe-strangest-bug-of-ie-ive-ever-seen%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F09%2F07%2Fthe-strangest-bug-of-ie-ive-ever-seen%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>We have a web application written in <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">asp.net</a> and we experienced really strange problem after recent modification. We have a strange behavior in IE9 with redirects and after some inspection we found a really strange fact (that almost make us lost 2 hours of work). To recreate this situation simply create a new page in an <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">asp.net</a> site, and put this code in it.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">form</span> <span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">=&quot;form1&quot;</span> <span style="color: rgb(255, 0, 0);">runat</span><span style="color: rgb(0, 0, 255);">=&quot;server&quot;</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">label</span> <span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">=&quot;queuecount&quot;</span>  <span style="color: rgb(0, 0, 255);">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">asp:ImageButton</span> <span style="color: rgb(255, 0, 0);">ID</span><span style="color: rgb(0, 0, 255);">=&quot;ImageButton1&quot;</span> <span style="color: rgb(255, 0, 0);">runat</span><span style="color: rgb(0, 0, 255);">=&quot;server&quot;</span> <span style="color: rgb(0, 0, 255);">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">a</span> <span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">=&quot;http://www.microsoft.com&quot;</span><span style="color: rgb(0, 0, 255);">&gt;</span>microsoft<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span> <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span> <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">form</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>Now simply run this page with fiddler opened, you should see a simple broken image and a standard anchor tag. Now click on the link and look at what happened with fiddler.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/09/image.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/09/image_thumb.png" width="480" height="106" /></a></p>
<p>Internet explorer issue the request to <a target="_blank" href="http://www.microsoft.com">www.microsoft.com</a>, but immediately after he request the original asp.ner page in POST. The order of the two requests varies, sometimes the <a target="_blank" title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a>.aspx POST is done before requesting the link, and the user navigates to <a href="http://www.microsoft.com">www.microsoft.com</a>, but if the order is reversed (as appears in the above image) the user remains on the original page wondering on what is happening.</p>
<p>Using <a title="Firefox" href="http://www.codewrecks.com/blog/index.php/tag/firefox/">Firefox</a>, Opera or Chrome do not produce any strange result, it seems only a bug of Internet Explorer. If you remove the HTML label tag or the image button everything works correctly, but it seems that if you have a label and an imagebutton, a simple anchor link in the page causes an unnecessary postback to the same page. </p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/09/07/the-strangest-bug-of-ie-ive-ever-seen/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ASp.NEt handlers locked down in Windows 7 IIS</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/08/04/asp-net-handlers-locked-down-in-windows-7-iis/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/08/04/asp-net-handlers-locked-down-in-windows-7-iis/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 16:12:09 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[IIS]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/08/04/asp-net-handlers-locked-down-in-windows-7-iis/</guid>
		<description><![CDATA[Iâ€™m configuring a site on a new virtual machine and when I browse to a site, IIS gives me this error This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=&#34;Deny&#34;), or set explicitly by a location tag with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F04%2Fasp-net-handlers-locked-down-in-windows-7-iis%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F04%2Fasp-net-handlers-locked-down-in-windows-7-iis%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET,IIS&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Iâ€™m configuring a site on a new <a title="virtual machine" href="http://www.codewrecks.com/blog/index.php/tag/virtual-machine/">virtual machine</a> and when I browse to a site, IIS gives me this error</p>
<blockquote><p>This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=&quot;Deny&quot;), or set explicitly by a location tag with overrideMode=&quot;Deny&quot; or the legacy allowOverride=&quot;false&quot;.</p>
</blockquote>
<p>I donâ€™t know why, but on this Windows 7 <a title="virtual machine" href="http://www.codewrecks.com/blog/index.php/tag/virtual-machine/">virtual machine</a> it seems that this section of the configuration is locked. If you find this error you can simply open an elevated permission command prompt, go to the <em>C:WindowsSystem32inetsrv</em> directory and type the commands</p>
<p><em>appcmd unlock config -section:system.webServer/handlers     <br />appcmd unlock config -section:system.webServer/modules</em></p>
<p>and everything works again <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Alk.   </p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/08/04/asp-net-handlers-locked-down-in-windows-7-iis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check if a user belong to a certain role in  ASP.Net</title>
		<link>http://www.codewrecks.com/blog/index.php/2010/10/13/how-to-check-if-a-user-belong-to-a-certain-role-in-asp-net/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2010/10/13/how-to-check-if-a-user-belong-to-a-certain-role-in-asp-net/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 15:02:54 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2010/10/13/how-to-check-if-a-user-belong-to-a-certain-role-in-asp-net/</guid>
		<description><![CDATA[This question is really simple to answerâ€¦ or no? Suppose you need to verify, in a service, if the user belongs to the xxxx group, and then take a different path of execution if the condition is true. if (Roles.IsUserInRole(&#34;xxxx&#34;)) { ... } Ok, this seems such a piece of innocent code, but actually it [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F10%2F13%2Fhow-to-check-if-a-user-belong-to-a-certain-role-in-asp-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F10%2F13%2Fhow-to-check-if-a-user-belong-to-a-certain-role-in-asp-net%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET,Security&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This question is really simple to answerâ€¦ or no? Suppose you need to verify, in a service, if the user belongs to the xxxx group, and then take a different path of execution if the condition is true.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">if</span> (Roles.IsUserInRole(<span style="color: #006080">&quot;xxxx&quot;</span>))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ...</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></div>
</div>
<p>Ok, this seems such a piece of innocent code, but actually it caused me a bad bug. The reason is really simple, the same service is called from a program written in windows forms, (a windows service) and a web site. The programmer that is developing the web site, took the service and add that checks in one function, and I begin to get exception from the code of the service. The reason is clear <a title="Asp.Net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">Asp.Net</a> roles and Membership are not configured in the windows program, nor I want to configure it.</p>
<p>To fix this, and in <a title="general" href="http://www.codewrecks.com/blog/index.php/category/general/">general</a> to verify roles of the current user, when you does not know in advance if the code would be called&#160; outside the context of a web application, we need to create the group xxxx in windows, assign the user that run the service or the program to this group and add this line at the very beginning of the winform program to use windows authentication. </p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);</pre>
<p><!--CRLF--></div>
</div>
<p>But this is not enough since the code in the service should not use the Roles.IsUserInRole() function because it needs to have membership configuration enabled. The solution is using this code instead of the above one.</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">var currentPrincipal = System.Threading.Thread.CurrentPrincipal;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">if</span> (currentPrincipal.IsInRole(<span style="color: #006080">&quot;xxxx&quot;</span>)</pre>
<p><!--CRLF--></div>
</div>
<p>This works because in windows forms the CurrentPrincipal is a <a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal.aspx">WindowsPrincipal</a>, and thus it checks if the current user belong to the group xxxx, but when it is run from a web site, with <a target="_blank" title="ASP.NEt" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">ASP.NEt</a> membership configured, the principal is of type <a href="http://msdn.microsoft.com/en-us/library/system.web.security.roleprincipal.aspx">RolePrincipal</a> as you can verify from Fgure1</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/10/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/10/image_thumb1.png" width="642" height="165" /></a></p>
<p><strong>Figure 1:</strong><em> the current principal of code running in <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/tag/aspnet/">asp.net</a> sites with roles and membership configured is of type RolePrincipal.</em></p>
<p>and in this situation IsInRole() method verifies the user against <a title="ASP.Net" href="http://www.codewrecks.com/blog/index.php/tag/aspnet/">ASP.Net</a> roles.</p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2010/10/13/how-to-check-if-a-user-belong-to-a-certain-role-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deploy on remote machine during builds</title>
		<link>http://www.codewrecks.com/blog/index.php/2010/07/06/deploy-on-remote-machine-during-builds/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2010/07/06/deploy-on-remote-machine-during-builds/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 07:21:21 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Tools and library]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2010/07/06/deploy-on-remote-machine-during-builds/</guid>
		<description><![CDATA[Clearly Lab Management is really good to manage virtual environments and give a lot of flexibility on how to deploy your application on virtual machines, but if you do not have Lab Management you can still use the same technique to deploy application in remote computer during a build with the use of a simple [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F07%2F06%2Fdeploy-on-remote-machine-during-builds%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F07%2F06%2Fdeploy-on-remote-machine-during-builds%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Clearly <a target="_blank" title="Lab Management" href="http://www.codewrecks.com/blog/index.php/category/programming/team-foundation-server/lab-management-team-foundation-server-programming/">Lab Management</a> is really good to <a href="http://www.codewrecks.com/blog/index.php/2010/06/29/deploy-a-solution-and-a-database-in-a-lab-management-virtual-environment/">manage virtual environment</a>s and give a lot of flexibility on how to deploy your application on virtual machines, but if you do not have <a title="Lab Management" href="http://www.codewrecks.com/blog/index.php/category/programming/team-foundation-server/lab-management-team-foundation-server-programming/">Lab Management</a> you can still use the same technique to deploy application in remote computer during a build with the use of a simple scripts. The key is the ability to execute code on a remote computer with <a href="http://www.beyondlogic.org/solutions/remoteprocess/BeyondExec.htm">beyondexec</a> or similar tool. Suppose you need to deploy an application called demo, you have a <a target="_blank" title="Tfs" href="http://msdn.microsoft.com/en-us/vstudio/ff637362">Tfs</a> build called Demo, and you want to be able to deploy a specific build on a remote server, the solution could be this simple script</p>
<p><em><font size="2">@echo off</font></em></p>
<p><em><font size="2">set RemotePath= \10.0.0.220dropsdemo%1_PublishedWebsitesDemo_Package*.*       <br />set LocalPath=c:<a title="setup" href="http://www.codewrecks.com/blog/index.php/tag/setup/">setup</a>demo</font></em></p>
<p><em><font size="2">if exist %LocalPath% (       <br />rmdir /s /q %LocalPath%        <br />)        <br />mkdir %LocalPath%</font></em></p>
<p><em><font size="2">if not exist %RemotePath% (       <br />echo remote path %RemotePath% doesn&#8217;t exist        <br />goto Error        <br />)</font></em>    <br /><em><font size="2">xcopy /c %RemotePath% %LocalPath%.       <br />%LocalPath%Demo.deploy.cmd /Y</font></em></p>
<p><em><font size="2">echo Demo Copied to LocalPath %LocalPath%       <br />exit /b 0</font></em></p>
<p><em><font size="2">:Error       <br />echo Unable to Copy Demo Scripts        <br />exit /b 1</font></em></p>
<p>The script is really simple, it simply compose the drop folder <a target="_blank" href="file://\10.0.0.220dropsdemo">\10.0.0.220dropsdemo</a> with the single argument of the batch to find the path where the build had dropped the packages, then it creates a local directory where to copy all deploy plackage and run it.</p>
<p>Now you can store this script in source control, and use it with a <a target="_blank" title="tfs" href="http://msdn.microsoft.com/en-us/vstudio/ff637362">tfs</a> build, to deploy on a remote server, you can for example run from a command prompt:</p>
<p><strong><em>beyondexecv2 \10.0.0.220 -p &quot;pa$$wordâ€ -c DeployDeployWeb.bat demo_20100607.3</em></strong></p>
<p>This will copy the script on the 10.0.0.220 server, and launch the script with the argument demo_20100607.3 (the build number), here is the result.</p>
<p><em><font size="1" face="Consolas">BeyondExec V2.05 &#8211; Spawn Remote Processes on Windows NT/2000/XP WorkStations.       <br />Copyright(C) 2002-2003 Craig.Peacock@beyondlogic.org        <br />[10.0.0.220] Establishing Connection . . .        <br />[10.0.0.220] BeyondExec service already installed on remote machine.        <br />[10.0.0.220] Copying DeployDeployWeb.bat to \10.0.0.220ADMIN$tempDeployWeb.bat        <br />[10.0.0.220] Command Successfully Issued to 10.0.0.220 on Pipe 1.        <br />[win-y4onzs094up] Process started, ProcessID = 9780, ThreadID = 9788        <br />\10.0.0.220dropsdemodemo_20100607.3_PublishedWebsitesDemo_PackageDemo.deploy-readme.txt        <br />\10.0.0.220dropsdemodemo_20100607.3_PublishedWebsitesDemo_PackageDemo.deploy.cmd        <br />\10.0.0.220dropsdemodemo_20100607.3_PublishedWebsitesDemo_PackageDemo.SetParameters.xml        <br />\10.0.0.220dropsdemodemo_20100607.3_PublishedWebsitesDemo_PackageDemo.SourceManifest.xml        <br />\10.0.0.220dropsdemodemo_20100607.3_PublishedWebsitesDemo_PackageDemo.zip        <br />5 File(s) copied        <br />=========================================================        <br />SetParameters from:        <br />&quot;c:<a title="setup" href="http://www.codewrecks.com/blog/index.php/tag/setup/">setup</a>demoDemo.SetParameters.xml&quot;        <br />You can change IIS Application Name, Physical path, connectionString        <br />or other deploy parameters in the above file.        <br />&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-        <br /> Start executing msdeploy.exe        <br />&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-        <br /> &quot;C:Program FilesIISMicrosoft Web Deploy\msdeploy.exe&quot; -source:package=&#8217;c:setupdemoDemo.zip&#8217; -dest:auto,includeAcls=&#8217;False&#8217; -verb:syn        <br />c -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:&quot;c:setupdemoDemo.SetParamet        <br />ers.xml&quot;        <br />Info: Adding sitemanifest (sitemanifest).        <br />Info: Updating createApp (Default Web Site/Demo_deploy).        <br />Info: Adding contentPath (Default Web Site/Demo_deploy).        <br />â€¦        <br />Info: Adding setAcl (Default Web Site/Demo_deploy).        <br />Total changes: 37 (36 added, 0 deleted, 1 updated, 0 parameters changed, 737702 bytes copied)        <br />[win-y4onzs094up] Process terminated with exit code 0 after 00:00:03.850s        <br />[win-y4onzs094up] Removing C:WindowstempDeployWeb.bat</font></em></p>
<p>As you can verify, the beyondexec tool was able to connect to remote machine, copied the deployweb.bat script on the remote machine and launched it with the right argument, now I can verify with IIS that the new web application was created in target machine. </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/07/image9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" class="wlDisabledImage" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/07/image_thumb9.png" width="322" height="265" /></a></p>
<p>Et voilÃ , we deployed a web application to a remote machine with a simple command. You can use this technique in a <a title="tfs build" href="http://www.codewrecks.com/blog/index.php/tag/tfs-build/">tfs build</a> to deploy to a remote server.</p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2010/07/06/deploy-on-remote-machine-during-builds/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Asp net and an HiddenField inside a disable panel</title>
		<link>http://www.codewrecks.com/blog/index.php/2010/04/15/asp-net-and-an-hiddenfield-inside-a-disable-panel/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2010/04/15/asp-net-and-an-hiddenfield-inside-a-disable-panel/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 11:38:34 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2010/04/15/asp-net-and-an-hiddenfield-inside-a-disable-panel/</guid>
		<description><![CDATA[Iâ€™ve hit a strange behavior of Hidden Field inside a disable asp.net panel, here is a simplified scenario that can reproduce my problem: 1: &#60;form id=&#34;form1&#34; runat=&#34;server&#34;&#62; 2: &#60;asp:HiddenField ID=&#34;hf1&#34; runat=&#34;server&#34; /&#62; 3: &#60;asp:TextBox ID=&#34;tb1&#34; runat=&#34;server&#34;&#62;&#60;/asp:TextBox&#62; 4: &#60;asp:Panel ID=&#34;Panel1&#34; runat=&#34;server&#34; Enabled=&#34;false&#34;&#62; 5: &#60;asp:HiddenField ID=&#34;hf2&#34; runat=&#34;server&#34; /&#62; 6: &#60;asp:TextBox ID=&#34;tb2&#34; runat=&#34;server&#34;&#62;&#60;/asp:TextBox&#62; 7: &#60;/asp:Panel&#62; 8: &#60;asp:Label ID=&#34;Label1&#34; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F04%2F15%2Fasp-net-and-an-hiddenfield-inside-a-disable-panel%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F04%2F15%2Fasp-net-and-an-hiddenfield-inside-a-disable-panel%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Iâ€™ve hit a strange behavior of Hidden Field inside a disable <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">asp.net</a> panel, here is a simplified scenario that can reproduce my problem:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;form1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:HiddenField</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;hf1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TextBox</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;tb1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:TextBox</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Panel</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Panel1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #ff0000">Enabled</span><span style="color: #0000ff">=&quot;false&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:HiddenField</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;hf2&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>        <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TextBox</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;tb2&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:TextBox</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:Panel</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Label</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Label1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">=&quot;Label&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:Label</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>    <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Button</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Button1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">=&quot;Button&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>This is a simple piece of code with two textbox, and two hidden fields, but the important aspect is that two of them are inside an asp:panel that has disabled=â€trueâ€. Then I have a label and a simple Button. Here is the code behind.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Sub</span> PageLoad(<span style="color: #0000ff">ByVal</span> sender <span style="color: #0000ff">As</span> <span style="color: #0000ff">Object</span>, <span style="color: #0000ff">ByVal</span> e <span style="color: #0000ff">As</span> System.EventArgs) <span style="color: #0000ff">Handles</span> <span style="color: #0000ff">Me</span>.Load</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>      <span style="color: #0000ff">If</span> (Page.IsPostBack = <span style="color: #0000ff">False</span>) <span style="color: #0000ff">then</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>          hf1.Value = <span style="color: #006080">&quot;HF1&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>          hf2.Value = <span style="color: #006080">&quot;HF2&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>          tb1.Text = <span style="color: #006080">&quot;TB1&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>          tb2.Text = <span style="color: #006080">&quot;TB2&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>      <span style="color: #0000ff">End</span> <span style="color: #0000ff">If</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Sub</span> Page_PreRender(<span style="color: #0000ff">ByVal</span> sender <span style="color: #0000ff">As</span> <span style="color: #0000ff">Object</span>, <span style="color: #0000ff">ByVal</span> e <span style="color: #0000ff">As</span> System.EventArgs) <span style="color: #0000ff">Handles</span> <span style="color: #0000ff">Me</span>.PreRender</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>      Label1.Text = <span style="color: #006080">&quot;hf1=&quot;</span> &amp; hf1.Value &amp; <span style="color: #006080">&quot;&lt;br /&gt; hf2=&quot;</span> &amp; hf2.Value</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #0000ff">End</span> Sub</pre>
<p><!--CRLF--></div>
</div>
<p>The code set values for all controls in the pageLoad, only at the first call, while in PreRender I dump the content of the hiddenfield in a label to have a visual clue of what is happening. This is the page when it is loaded for the first time.<a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image11.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb11.png" width="194" height="131" /></a> </p>
<p>Now I cause a postback pressing the Button, and the output become</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image12.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb12.png" width="207" height="130" /></a> </p>
<p>While the two texboxes mantain the value that I set by code in pageLoad, the <strong>hidden field inside the disabled panel has lost his value</strong>. The key to understand this behavior is to have a first look with fiddler at the request issued when the button is pressed. </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb13.png" width="253" height="132" /></a> </p>
<p>Clearly the browser send the content of the HTML Web Form and <em>it sends only the value for enabled control</em>, but how can the disabled textbox retain its value if the value is not passed with Request.Form parameters? The answer is: ViewState. Thanks to <a target="_blank" href="http://projects.nikhilk.net/WebDevHelper">Web Developement Helper</a> I can inspect viewstate</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image14.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb14.png" width="355" height="424" /></a> </p>
<p>As you can verify the content of the Disabled textbox is placed in viewstate, so it can be restored during a postback, but for the hiddenField, the value is not present in the viewState so the value is lost between postback because it is disabled. </p>
<p>This is the problem I have in a control that Iâ€™ve developed, I use hidden field to communicate value to <a title="jQuery" href="http://www.codewrecks.com/blog/index.php/category/frameworks/jquery/">jQuery</a> script, but when the user control is disabled, if I set a value by code in the hiddenField, value is null at the next postback, because the value is not passed with request.form nor it is inserted in the viewstate.</p>
<p>A solution to this problem can be found if you notice that even the first textbox does not have the text value stored in ViewState. This happens because the <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">asp.net</a> engine does not need it, because the value of the textbox can be retrieved from post parameters. Now if you add this handler to the code behind</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Sub</span> tb1_TextChanged(<span style="color: #0000ff">ByVal</span> sender <span style="color: #0000ff">As</span> <span style="color: #0000ff">Object</span>, <span style="color: #0000ff">ByVal</span> e <span style="color: #0000ff">As</span> System.EventArgs) <span style="color: #0000ff">Handles</span> tb1.TextChanged</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     ischanged = <span style="color: #0000ff">true</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">End</span> Sub</pre>
<p><!--CRLF--></div>
</div>
<p>the <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/tag/aspnet/">asp.net</a> engine need to know if the user changed the value of the textbox to raise the event and the situation is really different. If you take a look at viewstate </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image15.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb15.png" width="179" height="244" /></a> </p>
<p>value of Text property of the first texbox is now in the viewstate because <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/tag/aspnet/">asp.net</a> engine needs it to understand when to raise textboxchanged event. This bring me the solution for hiddenField, because I can simply handle the event.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">Protected</span> <span style="color: #0000ff">Sub</span> hf2_ValueChanged(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #0000ff">ByVal</span> sender <span style="color: #0000ff">As</span> <span style="color: #0000ff">Object</span>, </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">ByVal</span> e <span style="color: #0000ff">As</span> System.EventArgs) <span style="color: #0000ff">Handles</span> hf2.ValueChanged</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">End</span> Sub</pre>
<p><!--CRLF--></div>
</div>
<p>Now asp<a title=".net" href="http://www.codewrecks.com/blog/index.php/tag/net/">.net</a> insert the value of the Value property of HiddenField in the viewstate, because he needs to manage the ValueChanged event and the value of the HiddenField is persisted even if it is contained in a disabled Panel.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image16.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/04/image_thumb16.png" width="248" height="202" /></a> </p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Asp.Net" rel="tag">Asp.Net</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2010/04/15/asp-net-and-an-hiddenfield-inside-a-disable-panel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Three state checkbox in asp.net</title>
		<link>http://www.codewrecks.com/blog/index.php/2010/02/02/three-state-checkbox-in-asp-net/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2010/02/02/three-state-checkbox-in-asp-net/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 09:11:49 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2010/02/02/three-state-checkbox-in-asp-net/</guid>
		<description><![CDATA[Yesterday I need to implement in a quick way a three style checkbox in a project based on asp.net 3.5. The requirements stated that I must not spend too much time designing an entire new control, but the solution should be usable by other people with minimum impact. Here is the result I obtained. The [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F02%2F02%2Fthree-state-checkbox-in-asp-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F02%2F02%2Fthree-state-checkbox-in-asp-net%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ASP.NET&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Yesterday I need to implement in a quick way a three style checkbox in a project based on <a title="asp.net" href="http://www.codewrecks.com/blog/index.php/category/net-framework/aspnet/">asp.net</a> 3.5. The requirements stated that I must not spend too much time designing an entire new control, but the solution should be usable by other people with minimum impact. Here is the result I obtained.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2010/02/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2010/02/image_thumb.png" width="294" height="61" /></a></p>
<p>The checkbox can be, selected, not selected, or not used and when it is not used it is blue, like the one in windows forms. This was needed to satisfy a requirement, I need to make possible for the user to specify complex filters, and I have boolean fields on my entities where the user can ask for: filter only the one with field==true or field==false or no filter for that field. To achieve the result you can simply use a standard checkbox. </p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:CheckBox</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;chkCCSignaled&quot;</span> <span style="color: #ff0000">CssClass</span><span style="color: #0000ff">=&quot;threestate&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>The only stuff you need is that you need to assign the â€œthreestateâ€ style to the checkbox. (this satisfy the need of the minimuym impact, other developers that want to use that checkbox can simply add that css to a standard checkbox). The dirty work is done by an unobtrusive <a title="jquery" href="http://www.codewrecks.com/blog/index.php/category/frameworks/jquery/">jquery</a> <a title="javascript" href="http://www.codewrecks.com/blog/index.php/tag/javascript/">javascript</a> that gets injected with the masterpage.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> $(<span style="color: #0000ff">function</span>() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     $(<span style="color: #006080">'span.threestate'</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>         .log(<span style="color: #006080">'checkboxes'</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>         .each(<span style="color: #0000ff">function</span>() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>             <span style="color: #0000ff">var</span> thespan = $(<span style="color: #0000ff">this</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>             <span style="color: #0000ff">var</span> checkbox = $(thespan.children().get(0));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>             <span style="color: #0000ff">var</span> name = checkbox.attr(<span style="color: #006080">'id'</span>) + <span style="color: #006080">'_hf'</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>             <span style="color: #0000ff">var</span> innerhf = $(<span style="color: #006080">'&lt;input type=&quot;hidden&quot; name=&quot;'</span> + name + <span style="color: #006080">'&quot; id=&quot;'</span> + name + <span style="color: #006080">'&quot; /&gt;'</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>             thespan.prepend(innerhf);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>             <span style="color: #0000ff">var</span> innerslide = $(<span style="color: #006080">'&lt;div style=&quot;width:'</span> + thespan.width() + <span style="color: #006080">'px; height:'</span> + thespan.height() + <span style="color: #006080">'px&quot; class=&quot;chboverlay&quot; /&gt;'</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>             .css(<span style="color: #006080">'opacity'</span>, 0.8)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>                 .click(<span style="color: #0000ff">function</span>() {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>                     <span style="color: #008000">//debugger;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>                     <span style="color: #0000ff">if</span> (innerhf.val() == 2) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>                         $(<span style="color: #0000ff">this</span>).css(<span style="color: #006080">'opacity'</span>, 0.0)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>                         checkbox.attr(<span style="color: #006080">'checked'</span>, <span style="color: #0000ff">true</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>                         innerhf.val(1);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>                     } <span style="color: #0000ff">else</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>                         <span style="color: #0000ff">if</span> (innerhf.val() == 1) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>                             checkbox.attr(<span style="color: #006080">'checked'</span>, <span style="color: #0000ff">false</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>                             innerhf.val(0);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>                         } <span style="color: #0000ff">else</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>                             $(<span style="color: #0000ff">this</span>).css(<span style="color: #006080">'opacity'</span>, 0.8)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>                             checkbox.attr(<span style="color: #006080">'checked'</span>, <span style="color: #0000ff">false</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>                             innerhf.val(2);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>                         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>                     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>                 });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>             <span style="color: #0000ff">var</span> chkvalue = <span style="color: #006080">&quot;2&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>         </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>             <span style="color: #0000ff">if</span> (checkbox.parent().attr(<span style="color: #006080">'threestatevalue'</span>) != undefined) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>                 chkvalue = checkbox.parent().attr(<span style="color: #006080">'threestatevalue'</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>                 <span style="color: #0000ff">if</span> (chkvalue == <span style="color: #006080">&quot;0&quot;</span> || chkvalue == <span style="color: #006080">&quot;1&quot;</span>) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>                     innerslide.css(<span style="color: #006080">'opacity'</span>, 0.0)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>                 }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>             innerhf.val(chkvalue);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>             thespan.prepend(innerslide);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>         });</pre>
<p><!--CRLF--></div>
</div>
<p>The solution is quite simple, I create a new div with a specific class to overlay the checkbox and create the blue layer when the checkbox is in state â€œundefinedâ€. the tricky part is that I need to manage three possible value for the checkbox and I need to pass that value to the server during a postback, so I create dynamically an hidden input with the same id of the checkbox and the â€œ_hfâ€ string at the end. In that hidden field I store the actual value of the checkbox, 0 not selected, 1 selected and 2 undefined. The rest of the script is needed to manage the transition between states reacting to the click event of the checkbox.</p>
<p>In the server code I need to grab the three state value, so I created a simple extension method.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">Namespace</span> SiteCode.V2</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Module</span> ThreeStateCheckbox</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>         &lt;Extension()&gt; _</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>         <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Function</span> GetThreeStateValue(<span style="color: #0000ff">ByVal</span> cb <span style="color: #0000ff">As</span> CheckBox) <span style="color: #0000ff">As</span> Nullable(Of <span style="color: #0000ff">Boolean</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>             <span style="color: #0000ff">Dim</span> value <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span> = HttpContext.Current.Request.Form(cb.ClientID + <span style="color: #006080">&quot;_hf&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>             cb.Attributes(<span style="color: #006080">&quot;threestatevalue&quot;</span>) = value</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>             <span style="color: #0000ff">Select</span> <span style="color: #0000ff">Case</span> value</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                 <span style="color: #0000ff">Case</span> <span style="color: #006080">&quot;0&quot;</span> : <span style="color: #0000ff">Return</span> <span style="color: #0000ff">False</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                 <span style="color: #0000ff">Case</span> <span style="color: #006080">&quot;1&quot;</span> : <span style="color: #0000ff">Return</span> <span style="color: #0000ff">True</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>                 <span style="color: #0000ff">Case</span> <span style="color: #006080">&quot;2&quot;</span> : <span style="color: #0000ff">Return</span> <span style="color: #0000ff">Nothing</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>             <span style="color: #0000ff">End</span> <span style="color: #0000ff">Select</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     <span style="color: #0000ff">End</span> <span style="color: #0000ff">Module</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">End</span> Namespace</pre>
<p><!--CRLF--></div>
</div>
<p>This simple method is an extension method for the checkbox control, it simply grab the value of the dynamically generated hidden field from the Request.Form collection. After taking actual value, he add the attribute â€œthreestatevalueâ€ to the checkbox, because the client script should be able to restore the state of the checkbox after a postback. Now you can simply get the value with this code.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">ReadOnly</span> <span style="color: #0000ff">Property</span> IsCCChecked() <span style="color: #0000ff">As</span> Nullable(Of <span style="color: #0000ff">Boolean</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #0000ff">Get</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>         <span style="color: #0000ff">Return</span> chkCC.GetThreeStateValue()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">End</span> <span style="color: #0000ff">Get</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">End</span> Property</pre>
<p><!--CRLF--></div>
</div>
<p>With this structure the developer can simply add the css (as seen before) and use this extension method to grab the value, this without the need to author a whole new control.</p>
<p>I used this in a user control (that represents a complex filter and gets used in several pages), and this user control exposes the selection status to external control with readonly property that return Nullable(of Boolean). Thanks to extension method I can simply add the css to the checkbox, use GetThreeStateValue to grab the actual status of the checkbox and the game is done.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/asp.net" rel="tag">asp.net</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2010/02/02/three-state-checkbox-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

