<?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>Coding Still</title>
	<atom:link href="http://codingstill.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingstill.com</link>
	<description>Baby debugging steps...</description>
	<lastBuildDate>Wed, 15 May 2013 12:13:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>&#8216;&lt;name&gt;&#8217; is ambiguous in the namespace</title>
		<link>http://codingstill.com/2013/04/name-is-ambiguous-in-the-namespace/</link>
		<comments>http://codingstill.com/2013/04/name-is-ambiguous-in-the-namespace/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 19:49:05 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[Web Forms]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1347</guid>
		<description><![CDATA[There are times where this error appears in our web application, without a specific reason. '&#60;name&#62;' is ambiguous in the namespace '&#60;namespace&#62;' This error has the following error code Error ID: BC30560. One thing you could easily try is to change the value of the batch attribute in the &#60;compilation&#62; element of your Web.config file [...]]]></description>
				<content:encoded><![CDATA[<p>There are times where this error appears in our web application, without a specific reason.</p>
<pre>'&lt;name&gt;' is ambiguous in the namespace '&lt;namespace&gt;'</pre>
<p>This error has the following error code <strong>Error ID: BC30560</strong>.<br />
<span id="more-1347"></span><br />
One thing you could easily try is to change the value of the batch attribute in the &lt;compilation&gt; element of your Web.config file to false</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;compilation defaultLanguage<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;vb&quot;</span> debug<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;false&quot;</span> batch<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;false&quot;</span>&gt;</span></pre></td></tr></table></div>

<p>I recently had the &#8221;&lt;name&gt;&#8217; is ambiguous in the namespace&#8217; error appear in a very popular web site that has about 2 and a half million page views per month. It appeared only in the production server, not in the development or the testing server and nothing could make the error go away.<br />
Changing this setting, allowed the application to have a faster first load (by not loading all objects of its dll at once) and made the error go away. </p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2013/04/name-is-ambiguous-in-the-namespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things around the web #1</title>
		<link>http://codingstill.com/2013/02/things-around-the-web-1/</link>
		<comments>http://codingstill.com/2013/02/things-around-the-web-1/#comments</comments>
		<pubDate>Sun, 03 Feb 2013 18:22:50 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1274</guid>
		<description><![CDATA[This post gathers a few things I ran into the last few weeks and thought it would be fun to share it with you. Interesting Programmer Interrupted An article that describes how a programmer behaves when he is been interrupted when coding. It is an analysis based on 10,000 programming sessions recorded from 86 programmers [...]]]></description>
				<content:encoded><![CDATA[<blockquote><p>
This post gathers a few things I ran into the last few weeks and thought it would be fun to share it with you.<span id="more-1274"></span>
</p></blockquote>
<h3>Interesting</h3>
<p><a href="http://blog.ninlabs.com/2013/01/programmer-interrupted/" target="_blank">Programmer Interrupted</a><br />
An article that describes how a programmer behaves when he is been interrupted when coding. It is an analysis based on 10,000 programming sessions recorded from 86 programmers using Eclipse and Visual Studio and a survey of 414 programmers.</p>
<p><a href="http://www.grobmeier.de/the-10-rules-of-a-zen-programmer-03022012.html" target="_blank">The 10 rules of a Zen programmer</a><br />
Programmers can be a weird breed. The old Zen-masters already knew before hundreds of years, how today programmers should work. </p>
<p><a href="http://jscomplexity.org/" target="_blank">JS Complexity</a><br />
How does your js code score?</p>
<p><a href="http://webadvent.org/2012/more-code-more-problems-by-ed-finkler" target="_blank">More Code, More Problems</a><br />
Learn languages, not frameworks.<br />
Build small things.<br />
Less code is better than more.</p>
<p><a href="http://x-surface.tumblr.com/post/41282771026/x-surface-dont-believe-everything-you-read" target="_blank">X-Surface: Don’t believe everything you read.</a><br />
The games industry is the only one I can think of that will quite happily publish guesswork as news. So-called ‘analysts’ are no different &#8211; they make money by guessing. They’re about as much use as a ‘source’ as I am.</p>
<h3>Useful</h3>
<p><a href="http://www.bootstraphero.com/the-big-badass-list-of-twitter-bootstrap-resources" target="_blank">The Big Bad-ass list</a><br />
We all love Bootstrap, and has become the norm in web design. It has created its own ecosystem, and the above link contains 267 resources for it.</p>
<p><a href="http://singlepageappbook.com/" target="_blank">Single page apps in depth</a><br />
This free book is focusing on discussing patterns, implementation choices and decent practices while writing a single page app.</p>
<h3>Twitter</h3>
<p>From <a href="https://twitter.com/rstevens/status/236304911594295297" target="_blank">@rstevens</a><br />
Water is coffee with javascript turned off.<br />
From <a href="https://twitter.com/_astewart/status/270278240281714688" target="_blank">@_astewart</a><br />
“Boy, I really hope this web service returns XML” &#8211; Nobody ever.<br />
From <a href="https://twitter.com/BrettDruck/status/277200637224185857" target="_blank">@BrettDruck</a><br />
Fashion savvy people probably look at my outfit the same way I look at a person who opens their laptop and has the ask toolbar installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2013/02/things-around-the-web-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding the &#8216;A potentially dangerous Request.Form value was detected&#8217;</title>
		<link>http://codingstill.com/2013/01/avoiding-the-a-potentially-dangerous-request-form-value-was-detected/</link>
		<comments>http://codingstill.com/2013/01/avoiding-the-a-potentially-dangerous-request-form-value-was-detected/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 06:21:24 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Web Forms]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1225</guid>
		<description><![CDATA[This is a common error that ASP.NET developers have run into many times. We will see in this article a few ways on how to avoid it, both in Web Forms and MVC. This error occurs mostly because the data that are sent in the web server contain HTML. By default there a validation check [...]]]></description>
				<content:encoded><![CDATA[<p>This is a common error that ASP.NET developers have run into many times. We will see in this article a few ways on how to avoid it, both in Web Forms and MVC.<br />
<span id="more-1225"></span><br />
This error occurs mostly because the data that are sent in the web server contain HTML. By default there a validation check on all input so that our web application has a basic protection from XSS attacks.<br />
The easy way is to disable this validation process. This can be done by setting the below properties in the Web.config file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pages</span> <span style="color: #000066;">validateRequest</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In .NET 4.0, you need to change one more property.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pages</span> <span style="color: #000066;">validateRequest</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpRuntime</span> <span style="color: #000066;">requestValidationMode</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you want to apply this setting to a specific page, you can set it in the page directive of your .aspx file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ Page <span style="color: #000066;">Language</span>=<span style="color: #ff0000;">&quot;vb&quot;</span> <span style="color: #000066;">AutoEventWireup</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">CodeBehind</span>=<span style="color: #ff0000;">&quot;Example.aspx.cs&quot;</span> </span>
<span style="color: #009900;">                    <span style="color: #000066;">Inherits</span>=<span style="color: #ff0000;">&quot;Example.Example&quot;</span> <span style="color: #000066;">ValidateRequest</span>=<span style="color: #ff0000;">&quot;false&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<p>In case your web application is an ASP.NET MVC project and want to disable page validation for a specific view, you can set this property to its controller.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Post, ValidateInput<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Edit<span style="color: #008000;">&#40;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In MVC, you can actually declare which properties of your model you want to exclude from the validation process.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>HttpPost, ValidateInput<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span>, Exclude <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;YourFieldName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> ActionResult Edit<span style="color: #008000;">&#40;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Another approach is to set at the property of the Model to allow HTML content.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>AllowHtml<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Description <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Going back to Web Forms, if you want to allow HTML / JavaScript for a specific field, there is no direct way to do it. A nice trick you could do, is to encode the HTML in the client side and then decode it in the server side.<br />
The client side code</p>
<div class="control-group"></div>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// The event to escape the data and store in our HiddenField</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.allow_html textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=&quot;hidden&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>escape<span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// The code to unescape the code and set it in our textbox</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.allow_html textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>idx<span style="color: #339933;">,</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=&quot;hidden&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    jQuery<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>unescape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The server side code</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// encode the data</span>
HtmlCodeHiddenField<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span> <span style="color: #008000;">=</span> Uri<span style="color: #008000;">.</span><span style="color: #0000FF;">EscapeDataString</span><span style="color: #008000;">&#40;</span>EscapedHtml<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// decode the data</span>
<span style="color: #6666cc; font-weight: bold;">string</span> myHtml <span style="color: #008000;">=</span> Uri<span style="color: #008000;">.</span><span style="color: #0000FF;">UnescapeDataString</span><span style="color: #008000;">&#40;</span>HtmlCodeHiddenField<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you want to read more about this subject, read <a href="http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client" target="_blank">this article</a> from Stack Overflow.</p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2013/01/avoiding-the-a-potentially-dangerous-request-form-value-was-detected/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting video information from YouTube and Vimeo</title>
		<link>http://codingstill.com/2013/01/getting-video-information-from-youtube-and-vimeo/</link>
		<comments>http://codingstill.com/2013/01/getting-video-information-from-youtube-and-vimeo/#comments</comments>
		<pubDate>Mon, 21 Jan 2013 01:15:51 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[.NET Development]]></category>
		<category><![CDATA[videos]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1200</guid>
		<description><![CDATA[YouTube and Vimeo provide a data API that you can use to get information for their videos. YouTube requires the use of an API key and you can get one for free. Click here to see the steps required to obtain a YouTube API key. Vimeo on the other hand provides a simple API that [...]]]></description>
				<content:encoded><![CDATA[<p>YouTube and Vimeo provide a data API that you can use to get information for their videos. YouTube requires the use of an API key and you can get one for free. Click <a href="#ytapikey">here</a> to see the steps required to obtain a YouTube API key.<br />
<span id="more-1200"></span><br />
Vimeo on the other hand provides a simple API that does not require an API key. For authenticated read/write requests Vimeo provides the advanced API, which requires an API key. In this article we will see an example with the simple API. </p>
<p>In both cases we perform a web request and get back an xml file with the video&#8217;s data. Then our function parses the xml and loads the data to the object properties.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Video
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> ytKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Duration <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Title <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ThumbUrl <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> BigThumbUrl <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Description <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> VideoDuration <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Url <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> DateTime UploadDate <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> Video<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> YouTubeImport<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> VideoID<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> VideoDuration <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
      Net<span style="color: #008000;">.</span><span style="color: #0000FF;">WebClient</span> myDownloader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Net<span style="color: #008000;">.</span><span style="color: #0000FF;">WebClient</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      myDownloader<span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #6666cc; font-weight: bold;">string</span> myData <span style="color: #008000;">=</span> myDownloader<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://gdata.youtube.com/feeds/api/videos/&quot;</span> <span style="color: #008000;">+</span> VideoID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;?key=&quot;</span> <span style="color: #008000;">+</span> ytKey<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlDocument</span> myXml <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlDocument</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadXml</span><span style="color: #008000;">&#40;</span>myData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span> myNode <span style="color: #008000;">=</span> myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;entry&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      Title <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;title&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      ThumbUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://i.ytimg.com/vi/&quot;</span> <span style="color: #008000;">+</span> VideoID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/default.jpg&quot;</span><span style="color: #008000;">;</span>
      BigThumbUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://i.ytimg.com/vi/&quot;</span> <span style="color: #008000;">+</span> VideoID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/mqdefault.jpg&quot;</span><span style="color: #008000;">;</span>
      Description <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;content&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>Strings<span style="color: #008000;">.</span><span style="color: #0000FF;">Chr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;&lt;br /&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>Strings<span style="color: #008000;">.</span><span style="color: #0000FF;">Chr</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">13</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      UploadDate <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDateTime</span><span style="color: #008000;">&#40;</span>myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;published&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      VideoDuration <span style="color: #008000;">=</span> myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;yt:duration&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Attributes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;seconds&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">;</span>
      Duration <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>VideoDuration <span style="color: #008000;">/</span> <span style="color: #FF0000;">60</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;:&quot;</span> <span style="color: #008000;">+</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>VideoDuration <span style="color: #008000;">%</span> <span style="color: #FF0000;">60</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      Url <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.youtube.com/watch?v=&quot;</span> <span style="color: #008000;">+</span> VideoID<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> VimeoImport<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> VideoID<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> VideoDuration <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
      Net<span style="color: #008000;">.</span><span style="color: #0000FF;">WebClient</span> myDownloader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Net<span style="color: #008000;">.</span><span style="color: #0000FF;">WebClient</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      myDownloader<span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #6666cc; font-weight: bold;">string</span> myData <span style="color: #008000;">=</span> myDownloader<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://vimeo.com/api/v2/video/&quot;</span> <span style="color: #008000;">+</span> VideoID <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlDocument</span> myXml <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlDocument</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadXml</span><span style="color: #008000;">&#40;</span>myData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">XmlElement</span> myNode <span style="color: #008000;">=</span> myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;videos&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      Title <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;title&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      ThumbUrl <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;thumbnail_medium&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      PlayerImage <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;thumbnail_large&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      Description <span style="color: #008000;">=</span> myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;description&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      UploadDate <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDateTime</span><span style="color: #008000;">&#40;</span>myNode<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;upload_date&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      VideoDuration <span style="color: #008000;">=</span> myXml<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElementsByTagName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;duration&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
      Duration <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>VideoDuration <span style="color: #008000;">/</span> <span style="color: #FF0000;">60</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;:&quot;</span> <span style="color: #008000;">+</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>VideoDuration <span style="color: #008000;">%</span> <span style="color: #FF0000;">60</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      FileName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://vimeo.com/&quot;</span> <span style="color: #008000;">+</span> VideoID<span style="color: #008000;">;</span>
      EmbedCode <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
      Url <span style="color: #008000;">=</span> FileName<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">false</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><b>Note</b>: The Vimeo API can also give its data in a JSON or PHP format. This can be done easily by changing the URL&#8217;s suffix from &#8216;.xml&#8217; to &#8216;.json&#8217; or &#8216;.php&#8217;</p>
<h2 id="ytapikey">How to get a YouTube API key</h2>
<ol>
<li>First, you need to login to <a href="https://code.google.com/apis/console/">https://code.google.com/apis/console/</a>. </li>
<li>Then click on <b>Services</b> and scroll down to find the row for the <b>YouTube Data API v3</b>. </li>
<li>Set its status to ON and accept it&#8217;s Terms of Use. </li>
<li>Then click on <b>API Access</b> and in the <b>Simple API Access</b> area generate a new browser API key.</li>
</ol>
<p>Note that this API key is not only for YouTube, but for many Google services such as Google Maps API, Custom Search API, etc. </p>
<h3>Links</h3>
<ul>
<li><a href="https://developers.google.com/youtube/getting_started" target="_blank">YouTube API &#8211; Getting Started Guide</a></li>
<li><a href="https://code.google.com/apis/console/">Google API Console</a></li>
<li><a href="http://developer.vimeo.com/apis" target="_blank">Vimeo API</a></li>
<li><a href="http://developer.vimeo.com/apis/simple" target="_blank">Vimeo Simple API</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2013/01/getting-video-information-from-youtube-and-vimeo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set mime types for web fonts in IIS</title>
		<link>http://codingstill.com/2013/01/set-mime-types-for-web-fonts-in-iis/</link>
		<comments>http://codingstill.com/2013/01/set-mime-types-for-web-fonts-in-iis/#comments</comments>
		<pubDate>Sun, 13 Jan 2013 09:53:01 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[IIS]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1187</guid>
		<description><![CDATA[A typical @font-face declaration includes a .eot file, a .svg file, a .ttf file and a .woff file. If your IIS does not serve any of the previous file types, you need to set the appropriate mime type. Simply add the following MIME type declarations via IIS Manager (HTTP Headers tab of website properties): .eot [...]]]></description>
				<content:encoded><![CDATA[<p>A typical @font-face declaration includes a .eot file, a .svg file, a .ttf file and a .woff file. If your IIS does not serve any of the previous file types, you need to set the appropriate mime type.<br />
<span id="more-1187"></span><br />
Simply add the following MIME type declarations via IIS Manager (HTTP Headers tab of website properties):</p>
<pre>
<strong>.eot</strong>   application/vnd.ms-fontobject
<strong>.ttf</strong>   application/octet-stream
<strong>.svg</strong>   image/svg+xml
<strong>.woff</strong>  application/x-woff
</pre>
<p>If you do not have access to the IIS Manager, you can add these declarations in your Web.config file, in the &lt;system.webServer&gt; section.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;staticContent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mimeMap</span> <span style="color: #000066;">fileExtension</span>=<span style="color: #ff0000;">&quot;.eot&quot;</span> <span style="color: #000066;">mimeType</span>=<span style="color: #ff0000;">&quot;application/vnd.ms-fontobject&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mimeMap</span> <span style="color: #000066;">fileExtension</span>=<span style="color: #ff0000;">&quot;.ttf&quot;</span> <span style="color: #000066;">mimeType</span>=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mimeMap</span> <span style="color: #000066;">fileExtension</span>=<span style="color: #ff0000;">&quot;.svg&quot;</span> <span style="color: #000066;">mimeType</span>=<span style="color: #ff0000;">&quot;image/svg+xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mimeMap</span> <span style="color: #000066;">fileExtension</span>=<span style="color: #ff0000;">&quot;.woff&quot;</span> <span style="color: #000066;">mimeType</span>=<span style="color: #ff0000;">&quot;application/x-woff&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/staticContent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>Update</strong>: These settings will help you if the <a href="http://fortawesome.github.io/Font-Awesome/" target="_blank">Font Awesome</a> fonts are not showing in IE 9 or 10. </p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2013/01/set-mime-types-for-web-fonts-in-iis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Centering images horizontally and vertically</title>
		<link>http://codingstill.com/2012/12/centering-images-horizontally-and-vertically/</link>
		<comments>http://codingstill.com/2012/12/centering-images-horizontally-and-vertically/#comments</comments>
		<pubDate>Sat, 22 Dec 2012 03:10:24 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1103</guid>
		<description><![CDATA[In this article we will see how we can center an image to its parent element, regardless of its dimensions, with a small JavaScript plugin. ]]></description>
				<content:encoded><![CDATA[<p>In this article we will see how we can center an image to its parent element, regardless of its dimensions, with a small JavaScript plugin.</p>
<p>The plugin re-sizes the image to fit to its parent element and sets margin-top or margin-left so that the image centers and crops its sides. If the image is smaller than its container, we have the option to either stretch the image so it fills its container or just center to its container. In any case the image maintains its aspect ratio.</p>
<p><span id="more-1103"></span>In the case where both dimensions of the image are smaller than its parents dimensions and we don&#8217;t want to stretch the image we use a simple CSS solution, which is presented below.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.image_container</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.image_container</span> img <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>In all other cases the script calculates the top and left margin required to center the image.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">center_img</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">center_img</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>idx<span style="color: #339933;">,</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> jItem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">var</span> jImg <span style="color: #339933;">=</span> jItem.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">set_overflow</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                jItem.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'overflow'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">var</span> width <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>jItem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-width'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> jItem.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> jItem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-width'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">var</span> height <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>jItem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-height'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> jItem.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> jItem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-height'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">var</span> img_width <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>jImg.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-width'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> jImg.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> jImg.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-width'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">var</span> img_height <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>jImg.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-height'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> jImg.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> jImg.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-height'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">&lt;=</span> img_width <span style="color: #339933;">&amp;&amp;</span> height <span style="color: #339933;">&lt;=</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> height <span style="color: #339933;">&gt;</span> img_width <span style="color: #339933;">/</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'max-width'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>img_height <span style="color: #339933;">*</span> width <span style="color: #339933;">/</span> img_width<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> height<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> offset <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'max-height'</span><span style="color: #339933;">,</span> height <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>img_width <span style="color: #339933;">*</span> height <span style="color: #339933;">/</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> width<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> offset <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">must_fit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> height <span style="color: #339933;">&gt;</span> img_width <span style="color: #339933;">/</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> width <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>img_height <span style="color: #339933;">*</span> width <span style="color: #339933;">/</span> img_width<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> height<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> offset <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> height <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>img_width <span style="color: #339933;">*</span> height <span style="color: #339933;">/</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> width<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> offset <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">&gt;</span> img_width <span style="color: #339933;">&amp;&amp;</span> height <span style="color: #339933;">&gt;</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        jItem.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'position'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'relative'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'position'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'absolute'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'auto'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bottom'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'right'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'left'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">,</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>width <span style="color: #339933;">-</span> img_width<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        jImg.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">,</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>height <span style="color: #339933;">-</span> img_height<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">center_img</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            must_fit<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>       <span style="color: #006600; font-style: italic;">// The image will re-size in order to fill its container</span>
            set_overflow<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>    <span style="color: #006600; font-style: italic;">// Set this to true if the image container doesn't have the css property overflow:hidden;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The plugin has an option that defines whether we want the image to be stretched in order to fill its container.</p>
<div style="text-align: center; font-weight: bold; margin: 6px 0px; padding: 0 0 10px;">
<a href="http://codingstill.com/wp-content/uploads/2012/11/center_img.html" rel="lightbox">Click here for a simple demo</a>
</div>
<p>Note that you should call the plugin on the window.load event and not on the document.ready event, since the window.load event fires when the images of the document are loaded. Alternatively you can use the <a href="https://github.com/alexanderdickson/waitForImages" target="_blank">WaitForImages</a> plugin.<br />
On the other hand, if you know the dimensions of your image, you can assign the data-width and data-height attributes to the img tag and avoid the problematic event firing of the image loading. </p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2012/12/centering-images-horizontally-and-vertically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Export gridview or repeater to Excel</title>
		<link>http://codingstill.com/2012/11/export-gridview-or-repeater-to-excel/</link>
		<comments>http://codingstill.com/2012/11/export-gridview-or-repeater-to-excel/#comments</comments>
		<pubDate>Tue, 20 Nov 2012 18:47:39 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[data backup]]></category>
		<category><![CDATA[Web Forms]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1076</guid>
		<description><![CDATA[In this article we will see how we can use the GridView control for this task. This approach still limits us to use a GridView, so after that we will see how we can make a Repeater (or any other control) that renders an HTML table to an excel file.]]></description>
				<content:encoded><![CDATA[<p>In a previous article I wrote how one could <a title="Export data to excel for non-latin characters" href="http://codingstill.com/2011/04/export-data-to-excel-for-non-latin-characters/">export a Datatable to an excel file</a>. The provided function binded a DataGrid control to the Datatable and rendered it. The result was then flushed to the Response. One disadvantage of this approach is that we cannot use a GridView control, which is the control that replaced the DataGrid in .NET 2.0.</p>
<p><span id="more-1076"></span>In this article we will see how we can use the GridView control for this task. This approach still limits us to use a GridView, so after that we will see how we can make a Repeater (or any other control) that renders an HTML table to an excel file. The main advantage of this approach is that we don;&#8217;t present the plain data from the DataTable, but we can utilize the HTML formatting a GridView can provide and also not having to write a complex Sql Query to replace any computations we make on the GridView.</p>
<p>The code to export a GridView is here.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Sub</span> GridViewToExcel<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> myGridView <span style="color: #0000FF; font-weight: bold;">As</span> GridView, <span style="color: #0000FF; font-weight: bold;">ByVal</span> CurrentResponse <span style="color: #0000FF; font-weight: bold;">As</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">HttpResponse</span>, <span style="color: #0000FF; font-weight: bold;">ByVal</span> ExcelFileName <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Charset</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">ContentType</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;application/vnd.ms-excel&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;Content-Disposition&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;attachment;filename=&quot;</span> <span style="color: #000000;">&amp;</span>amp; ExcelFileName <span style="color: #000000;">&amp;</span>amp; <span style="color: #a52a2a; back-color: #fffacd;">&quot;.xls&quot;</span><span style="color: #000000;">&#41;</span>  
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">Dim</span> stringWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">IO</span><span style="color: #000000;">.</span><span style="color: #000000;">StringWriter</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> htmlWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">UI</span><span style="color: #000000;">.</span><span style="color: #000000;">HtmlTextWriter</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">&#41;</span>
    myGridView<span style="color: #000000;">.</span><span style="color: #000000;">RenderControl</span><span style="color: #000000;">&#40;</span>htmlWrite<span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Write</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">.</span><span style="color: #000000;">ToString</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #0000FF; font-weight: bold;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p>With this code snippet 2 errors will occur.</p>
<p>One is:</p>
<div style="padding-right: 4pt; padding-left: 4pt; background: white; padding-bottom: 1pt; padding-top: 1pt; border: windowtext 1pt solid;">
<p class="MsoNormal" style="background: white; margin: 0in 0in 10pt; line-height: normal; padding: 0in;"><span style="font-size: 18pt; color: red; font-family: 'Verdana',sans-serif;">Server Error in &#8216;/ASP.Net&#8217; Application. </span></p>
<hr size="1" width="100%" />
<p class="MsoNormal" style="background: white; margin: 0in 0in 10pt; line-height: normal; padding: 0in;"><em><span style="font-size: 14pt; color: maroon; font-family: 'Verdana',sans-serif;">Control &#8216;GridView1&#8242; of type &#8216;GridView&#8217; must be placed inside a form tag with runat=server.</span></em></p>
<p class="MsoNormal" style="background: white; margin: 0in 0in 10pt; padding: 0in;"><strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: 'Verdana',sans-serif;">Description: </span></strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: Arial,sans-serif;">An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</span></p>
<p><strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: 'Verdana',sans-serif;">Exception Details: </span></strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: Arial,sans-serif;">System.Web.HttpException: Control &#8216;GridView1&#8242; of type &#8216;GridView&#8217; must be placed inside a form tag with runat=server.</span></p>
</div>
<p>You can avoid this error by adding this function to your .aspx page.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Public</span> <span style="color: #0000FF; font-weight: bold;">Overrides</span> <span style="color: #0000FF; font-weight: bold;">Sub</span> VerifyRenderingInServerForm<span style="color: #000000;">&#40;</span>control <span style="color: #0000FF; font-weight: bold;">As</span> Control<span style="color: #000000;">&#41;</span>
	<span style="color: #008000; font-style: italic;">' Verifies that the control is rendered </span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p>The second is:</p>
<div style="padding-right: 4pt; padding-left: 4pt; padding-bottom: 1pt; padding-top: 1pt; border: windowtext 1pt solid;">
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; padding: 0in;"><span style="font-size: 18pt; color: red; font-family: 'Verdana',sans-serif;">Server Error in &#8216;ASP.Net&#8217; Application. </span></p>
<div class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; text-align: center; padding: 0in;" align="center"></div>
<hr noshade="noshade" size="1" width="100%" />
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; padding: 0in;"><em><span style="font-size: 14pt; color: maroon; font-family: 'Verdana',sans-serif;">RegisterForEventValidation can only be called during Render();</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; padding: 0in;"><strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: 'Verdana',sans-serif;">Description: </span></strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: Arial,sans-serif;">An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</span></p>
<p><strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: 'Verdana',sans-serif;">Exception Details: </span></strong><span style="font-size: 8.5pt; color: black; line-height: 115%; font-family: Arial,sans-serif;">System.InvalidOperationException: RegisterForEventValidation can only be called during Render();</span></p>
</div>
<p>You can avoid this error by setting the enableEventValidation property of your page to false. This can be done by setting this globally in the Web.config file or in the @Page Directive of your .aspx file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ Page <span style="color: #000066;">Language</span>=<span style="color: #ff0000;">&quot;VB&quot;</span> <span style="color: #000066;">AutoEventWireup</span>=<span style="color: #ff0000;">&quot;true&quot;</span> EnableEventValidation = <span style="color: #ff0000;">&quot;false&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<p>If you are don&#8217;t want to disable event validation you could put your GridView to an empty .aspx page and make the export there. Also, you could change the EnableEventValidation property via code. This can be done only in the PreInit event. There you could change its value to false.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Protected</span> <span style="color: #0000FF; font-weight: bold;">Sub</span> Page_PreInit<span style="color: #000000;">&#40;</span>sender <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">Object</span>, e <span style="color: #0000FF; font-weight: bold;">As</span> EventArgs<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">If</span><span style="color: #000000;">&#40;</span>Page<span style="color: #000000;">.</span><span style="color: #000000;">IsPostBack</span> <span style="color: #0000FF; font-weight: bold;">AndAlso</span> Request<span style="color: #000000;">.</span><span style="color: #000000;">Form</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;__EVENTTARGET&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;ExportToExcel&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
        <span style="color: #0000FF; font-weight: bold;">Me</span><span style="color: #000000;">.</span><span style="color: #000000;">Page</span><span style="color: #000000;">.</span><span style="color: #000000;">EnableEventValidation</span> <span style="color: #000000;">=</span> <span style="color: #0000FF; font-weight: bold;">False</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p>This is a nasty hack and also doesn&#8217;t change anything. So, we could check the control&#8217;s name that triggered the postback. Having a naming convention at least disables event validation in much less cases (still a hack though!).</p>
<p>The above solution doesn&#8217;t fit in cases where the data we want to export to an excel file are not rendered from a GridView control. If we have a Repeater or a combination of user controls that form an HTML table we can also create an excel file with it.<br />
In the case of the Repeater control, we simply render our repeater to an HtmlTextWriter and flush to the response.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Sub</span> RepeaterToExcel<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> myRepeater <span style="color: #0000FF; font-weight: bold;">As</span> Repeater, <span style="color: #0000FF; font-weight: bold;">ByVal</span> CurrentResponse <span style="color: #0000FF; font-weight: bold;">As</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">HttpResponse</span>, <span style="color: #0000FF; font-weight: bold;">ByVal</span> ExcelFileName <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Charset</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">ContentType</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;application/vnd.ms-excel&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;Content-Disposition&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;attachment;filename=&quot;</span> <span style="color: #000000;">&amp;</span>amp; ExcelFileName <span style="color: #000000;">&amp;</span>amp; <span style="color: #a52a2a; back-color: #fffacd;">&quot;.xls&quot;</span><span style="color: #000000;">&#41;</span>  
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">Dim</span> stringWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">IO</span><span style="color: #000000;">.</span><span style="color: #000000;">StringWriter</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> htmlWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">UI</span><span style="color: #000000;">.</span><span style="color: #000000;">HtmlTextWriter</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">&#41;</span>
    myRepeater<span style="color: #000000;">.</span><span style="color: #000000;">RenderControl</span><span style="color: #000000;">&#40;</span>htmlWrite<span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Write</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">.</span><span style="color: #000000;">ToString</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #0000FF; font-weight: bold;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p>Finally, if you have a different control you can use the function below. If you have a combination of user controls that form an HTML table you wrap them in a PlaceHolder and use the same function.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Sub</span> ControlToExcel<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> myControl <span style="color: #0000FF; font-weight: bold;">As</span> Control, <span style="color: #0000FF; font-weight: bold;">ByVal</span> CurrentResponse <span style="color: #0000FF; font-weight: bold;">As</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">HttpResponse</span>, <span style="color: #0000FF; font-weight: bold;">ByVal</span> ExcelFileName <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Charset</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">ContentType</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;application/vnd.ms-excel&quot;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;Content-Disposition&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;attachment;filename=&quot;</span> <span style="color: #000000;">&amp;</span>amp; ExcelFileName <span style="color: #000000;">&amp;</span>amp; <span style="color: #a52a2a; back-color: #fffacd;">&quot;.xls&quot;</span><span style="color: #000000;">&#41;</span>  
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">Dim</span> stringWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">IO</span><span style="color: #000000;">.</span><span style="color: #000000;">StringWriter</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> htmlWrite <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #0000FF; font-weight: bold;">New</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">UI</span><span style="color: #000000;">.</span><span style="color: #000000;">HtmlTextWriter</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">&#41;</span>
    myControl<span style="color: #000000;">.</span><span style="color: #000000;">RenderControl</span><span style="color: #000000;">&#40;</span>htmlWrite<span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #000000;">Write</span><span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">.</span><span style="color: #000000;">ToString</span><span style="color: #000000;">&#41;</span>
    CurrentResponse<span style="color: #000000;">.</span><span style="color: #0000FF; font-weight: bold;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2012/11/export-gridview-or-repeater-to-excel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clearing HTML tags and MS Word tags from text</title>
		<link>http://codingstill.com/2012/08/clearing-html-tags-and-ms-word-tags-from-text/</link>
		<comments>http://codingstill.com/2012/08/clearing-html-tags-and-ms-word-tags-from-text/#comments</comments>
		<pubDate>Tue, 28 Aug 2012 16:48:02 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[.NET Development]]></category>
		<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1036</guid>
		<description><![CDATA[It's a pretty common need for web developers to get the plain text from HTML text. Also, there are cases where users paste text from MS Word. In this article I will post a few simple functions that help me manipulate text that contains HTML and MS Word tags. ]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s a pretty common need for web developers to get the plain text from HTML text. Also, there are cases where users paste text from MS Word.</p>
<p>In this article I will post a few simple functions that help me manipulate text that contains HTML and MS Word tags in a various ways.</p>
<p><span id="more-1036"></span>All function are really small and use regular expressions.</p>
<h3>Clear HTML Tags</h3>
<p>This function replaces all tags with the ReplaceChar string. By default, the space character is used, because extra spaces don&#8217;t create any problem when showing the cleaned text to a web page.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> StripHTMLTags<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> HTMLToStrip <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>, <span style="color: #0000FF; font-weight: bold;">Optional</span> <span style="color: #0000FF; font-weight: bold;">ByVal</span> ReplaceChar <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot; &quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>HTMLToStrip <span style="color: #000000;">&lt;&gt;</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">.</span><span style="color: #000000;">Empty</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
        <span style="color: #0000FF; font-weight: bold;">Return</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>HTMLToStrip, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
    <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">.</span><span style="color: #000000;">Empty</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>Clear MS Word Tags</h3>
<p>The above function cannot clear all tags that MS Word contains. The function below does the trick. Combining both functions will give you a plain text.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> StripWordTags<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> HTMLToStrip <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>, <span style="color: #0000FF; font-weight: bold;">Optional</span> <span style="color: #0000FF; font-weight: bold;">ByVal</span> ReplaceChar <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot; &quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    StripWordTags <span style="color: #000000;">=</span> HTMLToStrip
    <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>HTMLToStrip <span style="color: #000000;">&lt;&gt;</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">.</span><span style="color: #000000;">Empty</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>HTMLToStrip, <span style="color: #a52a2a; back-color: #fffacd;">&quot;\n&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>StripWordTags, <span style="color: #a52a2a; back-color: #fffacd;">&quot;\r&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>StripWordTags, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&lt;!--.+?--&gt;&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>StripWordTags, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&amp;lt;!--.+?--&amp;gt;&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>StripWordTags, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&lt;!--.+?--&gt;&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
        StripWordTags <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>StripWordTags, <span style="color: #a52a2a; back-color: #fffacd;">&quot;]*&gt;&quot;</span>, ReplaceChar<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Function</span> StripAllTags<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> HTMLToStrip <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>, <span style="color: #0000FF; font-weight: bold;">Optional</span> <span style="color: #0000FF; font-weight: bold;">ByVal</span> ReplaceChar <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot; &quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    StripAllTags <span style="color: #000000;">=</span> StripWordTags<span style="color: #000000;">&#40;</span>HTMLToStrip<span style="color: #000000;">&#41;</span>
    StripAllTags <span style="color: #000000;">=</span> StripHTMLTags<span style="color: #000000;">&#40;</span>StripAllTags<span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>String To URL</h3>
<p>This function checks if a string is a URL and if not, it converts it to one. This comes handy to fix user input and a value is a website or domain.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> Str2Url<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> Str <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    Str2Url <span style="color: #000000;">=</span> Str
    <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>Str2Url <span style="color: #000000;">&lt;&gt;</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
        <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">Not</span> Str2Url<span style="color: #000000;">.</span><span style="color: #000000;">StartsWith</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;http://&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">And</span> <span style="color: #0000FF; font-weight: bold;">Not</span> Str2Url<span style="color: #000000;">.</span><span style="color: #000000;">StartsWith</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;https://&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
            Str2Url <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;http://&quot;</span> <span style="color: #000000;">&amp;</span> Str2Url
        <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
        <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">Not</span> Str2Url<span style="color: #000000;">.</span><span style="color: #000000;">EndsWith</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
            Str2Url <span style="color: #000000;">=</span> Str2Url <span style="color: #000000;">&amp;</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;/&quot;</span>
        <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>Websafe function</h3>
<p>This function replaces from a string all invalid characters that could cause a problem in a URL, e.g. when uploading a file and its original file name contains characters that will break a URL.<br />
Also some web servers cannot handle correctly file names with non latin characters. I have a function that converts all Greek file names to an equivalent name with Latin characters (and vice versa).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> WebSafe<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> Key <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    WebSafe <span style="color: #000000;">=</span> Greek2GrEnglish<span style="color: #000000;">&#40;</span>Key<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;^&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot; &quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;|&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000000;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    WebSafe <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>WebSafe, <span style="color: #a52a2a; back-color: #fffacd;">&quot;[^0-9|^a-z|^A-Z|^.|^_]&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
    WebSafe <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>WebSafe, <span style="color: #a52a2a; back-color: #fffacd;">&quot;\.+&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;.&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Function</span> Greek2GrEnglish<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> GreekStr <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> AllMatches<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;Α,A:Β,B:Γ,G:Δ,D:Ε,E:Ζ,Z:Η,I:Θ,TH:Ι,I:Κ,K:Λ,L:Μ,M:Ν,N:Ξ,KS:Ο,O:Π,P:Ρ,R:Σ,S:Τ,T:Υ,Y:Φ,F:Χ,X:Ψ,PS:Ω,W:Ά,A:Έ,E:Ή,I:Ί,I:Ό,O:Ύ,Y:Ώ,W:Ϊ,I:Ϋ,Y:ά,a:έ,e:ή,i:ί,i:ΰ,y:α,a:β,b:γ,g:δ,d:ε,e:ζ,z:η,i:θ,th:ι,i:κ,k:λ,l:μ,m:ν,n:ξ,ks:ο,o:π,p:ρ,r:ς,s:σ,s:τ,t:υ,y:φ,f:χ,x:ψ,ps:ω,w:ϊ,i:ϋ,y:ό,o:ύ,y:ώ,o:ΐ,i&quot;</span><span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;:&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> GreekChar, EnglishChar <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    Greek2GrEnglish <span style="color: #000000;">=</span> GreekStr
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">For</span> <span style="color: #0000FF; font-weight: bold;">Each</span> _Match <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #0000FF; font-weight: bold;">In</span> AllMatches
        GreekChar <span style="color: #000000;">=</span> _Match<span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">0</span><span style="color: #000000;">&#41;</span>
        EnglishChar <span style="color: #000000;">=</span> _Match<span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">1</span><span style="color: #000000;">&#41;</span>
        Greek2GrEnglish <span style="color: #000000;">=</span> Greek2GrEnglish<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>GreekChar, EnglishChar<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Function</span> GrEnglish2Greek<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> GrEnglishStr <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> AllMatches<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;Α,A:Β,B:Γ,G:Δ,D:Ε,E:Ζ,Z:Η,I:Θ,TH:Ι,I:Κ,K:Λ,L:Μ,M:Ν,N:Ξ,KS:Ο,O:Π,P:Ρ,R:Σ,S:Τ,T:Υ,Y:Φ,F:Χ,X:Ψ,PS:Ω,W:Ά,A:Έ,E:Ή,I:Ί,I:Ό,O:Ύ,Y:Ώ,W:Ϊ,I:Ϋ,Y:ά,a:έ,e:ή,i:ί,i:ΰ,y:α,a:β,b:γ,g:δ,d:ε,e:ζ,z:η,i:θ,th:ι,i:κ,k:λ,l:μ,m:ν,n:ξ,ks:ο,o:π,p:ρ,r:ς,s:σ,s:τ,t:υ,y:φ,f:χ,x:ψ,ps:ω,w:ϊ,i:ϋ,y:ό,o:ύ,y:ώ,o:ΐ,i&quot;</span><span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;:&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">Dim</span> GreekChar, EnglishChar <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    GrEnglish2Greek <span style="color: #000000;">=</span> GrEnglishStr
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">For</span> <span style="color: #0000FF; font-weight: bold;">Each</span> _Match <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span> <span style="color: #0000FF; font-weight: bold;">In</span> AllMatches
        GreekChar <span style="color: #000000;">=</span> _Match<span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">0</span><span style="color: #000000;">&#41;</span>
        EnglishChar <span style="color: #000000;">=</span> _Match<span style="color: #000000;">.</span><span style="color: #000066;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">1</span><span style="color: #000000;">&#41;</span>
        GrEnglish2Greek <span style="color: #000000;">=</span> GrEnglish2Greek<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>EnglishChar, GreekChar<span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">Next</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>URLSafe function</h3>
<p>This function is a stricter version of the WebSafe function. I am using this function when I need to upload files the RackspaceCloud&#8217;s CDN platform, Cloud Files. Also, this function uses the &#8220;-&#8221; character as the default replace character. This is also useful to create URL titles for new posts in a CMS.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> UrlSafe<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> Key <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    UrlSafe <span style="color: #000000;">=</span> Regex<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span>Greek2GrEnglish<span style="color: #000000;">&#40;</span>Key<span style="color: #000000;">.</span><span style="color: #000000;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;([^_0-9a-zA-Z])&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">While</span> <span style="color: #000000;">&#40;</span>UrlSafe<span style="color: #000000;">.</span><span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;--&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        UrlSafe <span style="color: #000000;">=</span> UrlSafe<span style="color: #000000;">.</span><span style="color: #000066;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;--&quot;</span>, <span style="color: #a52a2a; back-color: #fffacd;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">While</span>
    UrlSafe <span style="color: #000000;">=</span> UrlSafe<span style="color: #000000;">.</span><span style="color: #000066;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>CutText function</h3>
<p>This function cuts a long text to a limit of characters, but also makes sure that the last word of the text will not be cut in half.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> CutText<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> SomeText <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>, <span style="color: #0000FF; font-weight: bold;">Optional</span> <span style="color: #0000FF; font-weight: bold;">ByVal</span> Length <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">Integer</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">200</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    SomeText <span style="color: #000000;">=</span> Functions<span style="color: #000000;">.</span><span style="color: #000000;">StripAllTags</span><span style="color: #000000;">&#40;</span>SomeText<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>SomeText<span style="color: #000000;">.</span><span style="color: #000000;">Length</span> <span style="color: #000000;">&lt;</span> Length <span style="color: #000000;">+</span> <span style="color: #a52a2a; back-color: #fffacd;">1</span> <span style="color: #0000FF; font-weight: bold;">Or</span> Length <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span>
        <span style="color: #0000FF; font-weight: bold;">Return</span> SomeText
    <span style="color: #0000FF; font-weight: bold;">Else</span>
        <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #000000;">&#40;</span>SomeText<span style="color: #000000;">.</span><span style="color: #000000;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">0</span>, Length <span style="color: #000000;">+</span> SomeText<span style="color: #000000;">.</span><span style="color: #000000;">Substring</span><span style="color: #000000;">&#40;</span>Length<span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000000;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">If</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<h3>GetPathFromImageTag function</h3>
<p>This function finds an &lt;img&gt; HTML tag and returns the path from the src attribute</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Function</span> GetPathFromImageTag<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> ImageTag <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
    <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">Not</span> ImageTag<span style="color: #000000;">.</span><span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;src=&quot;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #6a5acd;">String</span><span style="color: #000000;">.</span><span style="color: #000000;">Empty</span>
&nbsp;
    GetPathFromImageTag <span style="color: #000000;">=</span> ImageTag<span style="color: #000000;">.</span><span style="color: #000000;">Substring</span><span style="color: #000000;">&#40;</span>ImageTag<span style="color: #000000;">.</span><span style="color: #000000;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;src=&quot;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">+</span> <span style="color: #a52a2a; back-color: #fffacd;">5</span><span style="color: #000000;">&#41;</span>
    GetPathFromImageTag <span style="color: #000000;">=</span> GetPathFromImageTag<span style="color: #000000;">.</span><span style="color: #000000;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">0</span>, GetPathFromImageTag<span style="color: #000000;">.</span><span style="color: #000000;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span></pre></td></tr></table></div>

<p>All functions are simple and provide basic functionality. I hope that you will find them useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2012/08/clearing-html-tags-and-ms-word-tags-from-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The case of the 500-mile email</title>
		<link>http://codingstill.com/2012/05/the-case-of-the-500-mile-email/</link>
		<comments>http://codingstill.com/2012/05/the-case-of-the-500-mile-email/#comments</comments>
		<pubDate>Sat, 05 May 2012 14:03:41 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=1025</guid>
		<description><![CDATA[Here&#8217;s a problem that *sounded* impossible&#8230; I almost regret posting the story to a wide audience, because it makes a great tale over drinks at a conference. The story is slightly altered in order to protect the guilty, elide over irrelevant and boring details, and generally make the whole thing more entertaining. I was working [...]]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s a problem that *sounded* impossible&#8230; I almost regret posting the story to a wide audience, because it makes a great tale over drinks at a conference. <img src='http://codingstill.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  The story is slightly altered in order to protect the guilty, elide over irrelevant and boring details, and generally make the whole thing more entertaining.</p>
<p>I was working in a job running the campus email system some years ago when I got a call from the chairman of the statistics department.</p>
<p>&#8220;<strong>We&#8217;re having a problem sending email out of the department.</strong>&#8221;</p>
<p>&#8220;<strong>What&#8217;s the problem?</strong>&#8221; I asked.</p>
<p>&#8220;<strong>We can&#8217;t send mail more than 500 miles</strong>,&#8221; the chairman explained.</p>
<p>I choked on my latte. &#8220;<strong>Come again?</strong>&#8221;<br />
<span id="more-1025"></span><br />
&#8220;<strong>We can&#8217;t send mail farther than 500 miles from here,</strong>&#8221; he repeated. &#8220;<strong>A little bit more, actually. Call it 520 miles. But no farther.</strong>&#8221;</p>
<p>&#8220;<strong>Um&#8230; Email really doesn&#8217;t work that way, generally</strong>,&#8221; I said, trying to keep panic out of my voice. One doesn&#8217;t display panic when speaking to a department chairman, even of a relatively impoverished department like statistics. &#8220;<strong>What makes you think you can&#8217;t send mail more than 500 miles?</strong>&#8221;</p>
<p>&#8220;<strong>It&#8217;s not what I *think*,</strong>&#8221; the chairman replied testily. &#8220;<strong>You see, when we first noticed this happening, a few days ago&#8211;</strong>&#8221;</p>
<p>&#8220;<strong>You waited a few DAYS?</strong>&#8221; I interrupted, a tremor tinging my voice. &#8220;<strong>And you couldn&#8217;t send email this whole time?</strong>&#8221;</p>
<p>&#8220;<strong>We could send email. Just not more than&#8211;</strong>&#8221;</p>
<p>&#8220;<strong>&#8211;500 miles, yes,</strong>&#8221; I finished for him, &#8220;<strong>I got that. But why didn&#8217;t you call earlier?</strong>&#8221;</p>
<p>&#8220;<strong>Well, we hadn&#8217;t collected enough data to be sure of what was going on until just now.</strong>&#8221; Right. This is the chairman of *statistics*. &#8220;<strong>Anyway, I asked one of the geostatisticians to look into it&#8211;</strong>&#8221;</p>
<p>&#8220;<strong>Geostatisticians&#8230;</strong>&#8221;</p>
<p>&#8220;<strong>&#8211;yes, and she&#8217;s produced a map showing the radius within which we can send email to be slightly more than 500 miles. There are a number of destinations within that radius that we can&#8217;t reach, either, or reach sporadically, but we can never email farther than this radius.</strong>&#8221;</p>
<p><strong>&#8220;I see,</strong>&#8221; I said, and put my head in my hands. &#8220;<strong>When did this start? A few days ago, you said, but did anything change in your systems at that time?</strong>&#8221;</p>
<p>&#8220;<strong>Well, the consultant came in and patched our server and rebooted it. But I called him, and he said he didn&#8217;t touch the mail system.</strong>&#8221;</p>
<p>&#8220;<strong>Okay, let me take a look, and I&#8217;ll call you back,</strong>&#8221; I said, scarcely believing that I was playing along. It wasn&#8217;t April Fool&#8217;s Day. I tried to remember if someone owed me a practical joke.</p>
<p>I logged into their department&#8217;s server, and sent a few test mails. This was in the Research Triangle of North Carolina, and a test mail to my own account was delivered without a hitch. Ditto for one sent to Richmond, and Atlanta, and Washington. Another to Princeton (400 miles) worked.</p>
<p>But then I tried to send an email to Memphis (600 miles). It failed. Boston, failed. Detroit, failed. I got out my address book and started trying to narrow this down. New York (420 miles) worked, but Providence (580 miles) failed.</p>
<p>I was beginning to wonder if I had lost my sanity. I tried emailing a friend who lived in North Carolina, but whose ISP was in Seattle. Thankfully, it failed. If the problem had had to do with the geography of the human recipient and not his mail server, I think I would have broken down in tears.</p>
<p>Having established that&#8211;unbelievably&#8211;the problem as reported was true, and repeatable, I took a look at the sendmail.cf file. It looked fairly normal. In fact, it looked familiar.</p>
<p>I diffed it against the sendmail.cf in my home directory. It hadn&#8217;t been altered&#8211;it was a sendmail.cf I had written. And I was fairly certain I hadn&#8217;t enabled the &#8220;FAIL_MAIL_OVER_500_MILES&#8221; option. At a loss, I telnetted into the SMTP port. The server happily responded with a SunOS sendmail banner.</p>
<p>Wait a minute&#8230; a SunOS sendmail banner? At the time, Sun was still shipping Sendmail 5 with its operating system, even though Sendmail 8 was fairly mature. Being a good system administrator, I had standardized on Sendmail 8. And also being a good system administrator, I had written a sendmail.cf that used the nice long self-documenting option and variable names available in Sendmail 8 rather than the cryptic punctuation-mark codes that had been used in Sendmail 5.</p>
<p>The pieces fell into place, all at once, and I again choked on the dregs of my now-cold latte. When the consultant had &#8220;patched the server,&#8221; he had apparently upgraded the version of SunOS, and in so doing *downgraded* Sendmail. The upgrade helpfully left the sendmail.cf alone, even though it was now the wrong version.</p>
<p>It so happens that Sendmail 5&#8211;at least, the version that Sun shipped, which had some tweaks&#8211;could deal with the Sendmail 8 sendmail.cf, as most of the rules had at that point remained unaltered. But the new long configuration options&#8211;those it saw as junk, and skipped. And the sendmail binary had no defaults compiled in for most of these, so, finding no suitable settings in the sendmail.cf file, they were set to zero.</p>
<p>One of the settings that was set to zero was the timeout to connect to the remote SMTP server. Some experimentation established that on this particular machine with its typical load, a zero timeout would abort a connect call in slightly over three milliseconds.</p>
<p>An odd feature of our campus network at the time was that it was 100% switched. An outgoing packet wouldn&#8217;t incur a router delay until hitting the POP and reaching a router on the far side. So time to connect to a lightly-loaded remote host on a nearby network would actually largely be governed by the speed of light distance to the destination rather than by incidental router delays.</p>
<p>Feeling slightly giddy, I typed into my shell:</p>
<pre>$ units
1311 units, 63 prefixes

You have: 3 millilightseconds
You want: miles
        * 558.84719
        / 0.0017893979</pre>
<p>&#8220;500 miles, or a little bit more.&#8221;</p>
<p>Trey Harris,<br />
From: <a href="http://www.ibiblio.org/harris/500milemail.html">http://www.ibiblio.org/harris/500milemail.html </a></p>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2012/05/the-case-of-the-500-mile-email/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET and Ajax. All about update panels, web methods, page methods and jQuery</title>
		<link>http://codingstill.com/2012/02/asp-net-and-ajax-all-about-update-panels-web-methods-page-methods-and-jquery/</link>
		<comments>http://codingstill.com/2012/02/asp-net-and-ajax-all-about-update-panels-web-methods-page-methods-and-jquery/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 15:26:42 +0000</pubDate>
		<dc:creator>_tasos</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[asp.net AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Forms]]></category>

		<guid isPermaLink="false">http://codingstill.com/?p=816</guid>
		<description><![CDATA[In this article, we will discuss what ASP.NET offers and a few tips on how to use them. Finally we will leave ASP.NET and see how we can achieve the same functionality with jQuery.]]></description>
				<content:encoded><![CDATA[<p>In modern web applications is expected that the implementation is more JavaScript based, so that its users have a more interactive experience. ASP.NET provides a few ways to the programmer to perform asynchronous requests to the web server.</p>
<p>In this article, we will discuss what ASP.NET offers and a few tips on how to use them. Finally we will leave ASP.NET and see how we can achieve the same functionality with jQuery.<br />
<span id="more-816"></span></p>
<ol class="nav">
<li><strong><a class="scroll" href="#update_panels">Update Panels</a></strong></li>
<li><strong><a class="scroll" href="#page_methods">Page Methods</a></strong></li>
<li><strong><a class="scroll" href="#web_methods">Web Methods</a></strong></li>
<li><strong><a class="scroll" href="#jquery">jQuery ajax</a></strong></li>
</ol>
<h2 id="update_panels">Update Panels</h2>
<p>Update panels are the oldest trick in the book for ASP.NET. It is the simplest way to have ajax functionality in your web application. We will not see them in depth, just mention a few basic points about them. Below is a simple example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;asp:UpdatePanel <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;SimpleUpdatePanel&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;ContentTemplate&gt;</span>
        <span style="color: #009900;">&lt;asp:Button <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;AjaxButton&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000066;">Text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Button&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;asp:Literal <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MessageLiteral&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>asp:Literal&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>ContentTemplate&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>asp:UpdatePanel&gt;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Protected</span> <span style="color: #0000FF; font-weight: bold;">Sub</span> AjaxButton_Click<span style="color: #000000;">&#40;</span>sender <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">Object</span>, e <span style="color: #0000FF; font-weight: bold;">As</span> EventArgs<span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Handles</span> AjaxButton<span style="color: #000000;">.</span><span style="color: #000000;">Click</span>
    MessageLiteral<span style="color: #000000;">.</span><span style="color: #000000;">Text</span> <span style="color: #000000;">=</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;Ajax update occured&quot;</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p>In code behind, you have access to the entire form. That is possible because when an update panel refreshes, the whole form is been posted to the web server. But only the markup of the update panel can change. So, if we try to change something that it is not inside our update panel the change will not happen.</p>
<p>It is that simple to have ajax functionality in your web application. We can also assign some event handlers so when an update panel refreshes we can execute some JavaScript code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> prm <span style="color: #339933;">=</span> Sys.<span style="color: #660066;">WebForms</span>.<span style="color: #660066;">PageRequestManager</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
prm.<span style="color: #660066;">add_beginRequest</span><span style="color: #009900;">&#40;</span>BeginRequestHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
prm.<span style="color: #660066;">add_endRequest</span><span style="color: #009900;">&#40;</span>EndRequestHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> BeginRequestHandler<span style="color: #009900;">&#40;</span>sender<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//code that runs when an UpdatePanel starts refreshing</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> EndRequestHandler<span style="color: #009900;">&#40;</span>sender<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//code that runs when an UpdatePanel has refreshed.</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With that level of abstraction, it is pretty clear than it is not a very optimal approach. If an update panel encloses a big part of the web form, it will be much slower. Even having a small update panel, the whole form is posted to the server, including the ViewState.</p>
<p>Generally it is not a good practice to have nested update panels in a big depth (up to 2 is generally ok). Finally, different update panels cannot refresh simultaneously. You can read about it (and a workaround for it) <a title="Simultaneous async requests with multiple Update Panels" href="http://codingstill.com/2010/08/simultaneous-async-requests-with-multiple-update-panels/">here</a>.</p>
<h2 id="page_methods">Page Methods</h2>
<p>With update panels we can update html sections of our web page. There is no direct way to get a value (e.g. a string or an integer) via ajax. For that, we can use page methods.<br />
Page methods are static methods declared in code behind and are exposed via JavaScript int the client. Page methods are much more lightweight since they don&#8217;t post nothing from the form, except what necessary parameters each method has.</p>
<p>In the following example you see a sample page method. You have to enable page methods in the ScriptManager. When calling the method in JavaScript you can set two function as attributes that will behave as success and error handlers of the ajax call.<br />
<strong>ASP.NET part</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;asp:ScriptManager <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ScriptManager1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> EnablePageMethods<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>asp:ScriptManager&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;asp:Button <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;PageMethodButton&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span> <span style="color: #000066;">Text</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Call Page Method&quot;</span> OnClientClick<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:call_page_method(); return false;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p><strong>JavaScript part</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> success<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ...
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> call_page_method<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    PageMethods.<span style="color: #660066;">HelloWorld</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> success<span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Code Behind part (vb.net)</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Imports</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">Public</span> <span style="color: #0000FF; font-weight: bold;">Class</span> _default
    <span style="color: #0000FF; font-weight: bold;">Inherits</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">UI</span><span style="color: #000000;">.</span><span style="color: #000000;">Page</span>
    <span style="color: #000000;">...</span>
&nbsp;
    <span style="color: #000000;">&lt;</span>WebMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
    <span style="color: #0000FF; font-weight: bold;">Public</span> <span style="color: #0000FF; font-weight: bold;">Shared</span> <span style="color: #0000FF; font-weight: bold;">Function</span> HelloWorld<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> LowerCase <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
        <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>LowerCase<span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;Hello World&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000000;">ToLower</span>
        <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;Hello World&quot;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Class</span></pre></td></tr></table></div>

<p>Page methods are lightweight. One disadvantage is that we can only use them in .aspx pages. We cannot declare a page method in a user control. Also, the JavaScript declaration of the method is embedded directly into the HTML. So, having many page methods will lead to bigger HTML files. Also, there is no way to declare them in a single place so that our entire web application can make use of them. </p>
<h2 id="web_methods">Web Methods</h2>
<p>Page methods can be very useful and practical. In the scenarios where we would like to have such methods declared globally we can use Web methods. We can declare our methods in a .asmx file. When we create the new .asmx file we need to uncomment one line of code, as the comments on the top of the page suggest.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">Imports</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span>
<span style="color: #0000FF; font-weight: bold;">Imports</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span><span style="color: #000000;">.</span><span style="color: #000000;">Protocols</span>
<span style="color: #0000FF; font-weight: bold;">Imports</span> System<span style="color: #000000;">.</span><span style="color: #000000;">ComponentModel</span>
&nbsp;
<span style="color: #008000; font-style: italic;">' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.</span>
<span style="color: #000000;">&lt;</span>System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Script</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span><span style="color: #000000;">.</span><span style="color: #000000;">ScriptService</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
<span style="color: #000000;">&lt;</span>System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span><span style="color: #000000;">.</span><span style="color: #000000;">WebService</span><span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">Namespace</span>:<span style="color: #000000;">=</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;http://tempuri.org/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
<span style="color: #000000;">&lt;</span>System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span><span style="color: #000000;">.</span><span style="color: #000000;">WebServiceBinding</span><span style="color: #000000;">&#40;</span>ConformsTo:<span style="color: #000000;">=</span>WsiProfiles<span style="color: #000000;">.</span><span style="color: #000000;">BasicProfile1_1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
<span style="color: #000000;">&lt;</span>ToolboxItem<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">False</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
<span style="color: #0000FF; font-weight: bold;">Public</span> <span style="color: #0000FF; font-weight: bold;">Class</span> myMethods
    <span style="color: #0000FF; font-weight: bold;">Inherits</span> System<span style="color: #000000;">.</span><span style="color: #000000;">Web</span><span style="color: #000000;">.</span><span style="color: #000000;">Services</span><span style="color: #000000;">.</span><span style="color: #000000;">WebService</span>
&nbsp;
    <span style="color: #000000;">&lt;</span>WebMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&gt;</span> _
    <span style="color: #0000FF; font-weight: bold;">Public</span> <span style="color: #0000FF; font-weight: bold;">Shared</span> <span style="color: #0000FF; font-weight: bold;">Function</span> HelloWorld<span style="color: #000000;">&#40;</span><span style="color: #0000FF; font-weight: bold;">ByVal</span> LowerCase <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">Boolean</span><span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">As</span> <span style="color: #6a5acd;">String</span>
        <span style="color: #0000FF; font-weight: bold;">If</span> <span style="color: #000000;">&#40;</span>LowerCase<span style="color: #000000;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">Then</span> <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #000000;">&#40;</span><span style="color: #a52a2a; back-color: #fffacd;">&quot;Hello World&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">.</span><span style="color: #000000;">ToLower</span>
        <span style="color: #0000FF; font-weight: bold;">Return</span> <span style="color: #a52a2a; back-color: #fffacd;">&quot;Hello World&quot;</span>
    <span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Function</span>
<span style="color: #0000FF; font-weight: bold;">End</span> <span style="color: #0000FF; font-weight: bold;">Class</span></pre></td></tr></table></div>

<p>In order to use the declared web methods, we need to register the .asmx service to our script manager. This can be in the asp.net markup or in the code behind.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;asp:ScriptManager <span style="color: #000066;">ID</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ScriptManager1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;Services&gt;</span>
        <span style="color: #009900;">&lt;asp:ServiceReference Path<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;~/myMethods.asmx&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>Services&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>asp:ScriptManager&gt;</span></pre></td></tr></table></div>

<p>Finally, to use the web method in our JavaScript code we need to write the full namespace. For example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> call_web_method<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    DemoWebApplication.<span style="color: #660066;">myMethods</span>.<span style="color: #660066;">HelloWorld</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> success<span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2 id="jquery">jQuery ajax</h2>
<p>With jQuery you can also make asynchronous calls to the server and gives you more flexibility while developing your application. You can see and example of jQuery.ajax in the following snippet.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/ajax_handlers/example-handler.ashx'</span><span style="color: #339933;">,</span>
    context<span style="color: #339933;">:</span> document.<span style="color: #660066;">body</span><span style="color: #339933;">,</span>
    cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">;</span>
    contentType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'application/json'</span><span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> server_response <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//This is the data the server returned</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
    error<span style="color: #339933;">:</span> handle_ajax_error
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Here is a error handler stating some useful values regarding the error occured.</span>
<span style="color: #000066; font-weight: bold;">function</span> handle_ajax_error<span style="color: #009900;">&#40;</span>xhr<span style="color: #339933;">,</span> ajaxOptions<span style="color: #339933;">,</span> thrownError<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error: '</span> <span style="color: #339933;">+</span> xhr.<span style="color: #660066;">responseText</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'__'</span> <span style="color: #339933;">+</span> thrownError <span style="color: #339933;">+</span> <span style="color: #3366CC;">'__'</span> <span style="color: #339933;">+</span> xhr.<span style="color: #660066;">statusText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There are a couple of things to consider. One is that you may get errors if the response&#8217;s data type is not what jQuery expects. Also, you should use the &#8216;cache&#8217; property and set it to false when you expect fresh data from the server. It&#8217;s default value is true. </p>
<p>For more information and options you can visit the <a href="http://api.jquery.com/jQuery.ajax/" target="_blank">documentation page</a> for jQuery.ajax.</p>
<h3>Comparing approaches</h3>
<p>The main question that could rise here is what approach should someone use. In every case there are some advantages and disadvantages. With ASP.NET tools the development is a bit easier and faster. With jQuery there are some issues that the developer will need to address (contentType issues, encoding issues, etc) while the ASP.NET tools handle these things internally.<br />
One disadvantage of the ASP.NET approach is that in any case the page must have one big form (runat=&#8221;server&#8221;). In a busy website this element might be better to be omitted and so jQuery is the (only) way to go.</p>
<p>In any case, the developer should know how to use jQuery in any case, since it is also used in ASP.NET MVC and most important it is not platform specific. </p>
<h3>Related articles</h3>
<p>We have seen how we can introduce ajax in our web application. The above article is an introduction for the above techniques. Below are a few articles related to the topic, that provide more specific information for them. The <a href="http://encosia.com/" target="_blank">blog of Dave Ward</a> has many useful and interesting articles regarding ASP.NET Ajax and jQuery.   </p>
<ul>
<li><a href="http://encosia.com/why-aspnet-ajax-updatepanels-are-dangerous/" target="_blank">Why ASP.NET AJAX UpdatePanels are dangerous</a></li>
<li><a href="http://codingstill.com/2010/08/simultaneous-async-requests-with-multiple-update-panels/">Simultaneous async requests with multiple Update Panels</a></li>
<li><a href="http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/" target="_blank">Using jQuery to directly call ASP.NET AJAX page methods</a></li>
<li><a href="http://encosia.com/easily-refresh-an-updatepanel-using-javascript/" target="_blank">Easily refresh an UpdatePanel, using JavaScript</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingstill.com/2012/02/asp-net-and-ajax-all-about-update-panels-web-methods-page-methods-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
