<?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>Frederik Vig - ASP.NET developer &#187; ASP.NET</title>
	<atom:link href="http://www.frederikvig.com/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frederikvig.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Jul 2010 14:08:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CSS and JavaScript compression and bundling</title>
		<link>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=css-and-javascript-compression-and-bundling</link>
		<comments>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 22:26:50 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1298</guid>
		<description><![CDATA[In the last post in Create an EPiServer site from scratch I go through some optimization tips and tricks from YSlow. One of these are CSS and JavaScript compression and bundling of files to create fewer HTTP requests and to send the least amount of data back to the client as possible, removing whitespaces, comments [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/">last post</a> in <a href="http://www.frederikvig.com/2009/12/introduction-create-an-episerver-site-from-scratch/">Create an EPiServer site from scratch</a> I go through some optimization tips and tricks from <a href="http://developer.yahoo.com/yslow/">YSlow</a>. One of these are CSS and JavaScript compression and bundling of files to create fewer HTTP requests and to send the least amount of data back to the client as possible, removing whitespaces, comments etc, that we don&#8217;t need to run our code. </p>
<p>In the past I&#8217;ve done this as part of my deployment process, but recently I&#8217;ve come across a tool called <a href="http://www.codethinked.com/post/2010/05/26/SquishIt-The-Friendly-ASPNET-JavaScript-and-CSS-Squisher.aspx">SquishIt</a>. Which basically is a wrapper for <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> (you can easily replace it with your compressor of choice). It is dead simple to use, you simply add a reference to SquishIt.Framework.dll in your project, and replace your old CSS and JavaScript links and script tags with code like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%=</span> Bundle.<span style="color: #0000FF;">Css</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/screen.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/print.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/mobile.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/combined-#.css&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;%=</span> Bundle.<span style="color: #0000FF;">JavaScript</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/belatedPNG-0.0.8a-min.js&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/ie6.js&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/ie6combined-#.js&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span></pre></div></div>

<p>Using a fluent interface makes it easy to add more file references, ending with the Render method that takes a parameter for where to store the combined and minimized file. </p>
<p>It is very easy to toggle SquishIt on and off. Simply set debug=&#8221;true&#8221; in your web.config file to turn it off and render CSS and JavaScript files as normal, great for when you need to debug your code. Set debug=&#8221;false&#8221; to enable compression and bundling again.</p>
<p>Best part is that it only took me 2 minutes to setup and add to my current project! Works like a charm so far <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/08/adding-css-and-javascript-files-dynamically-to-your-asp-net-page/" rel="bookmark" title="August 15, 2009">Adding CSS and JavaScript files dynamically to your ASP.NET page</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-newslist/" rel="bookmark" title="July 25, 2009">EPiServer web controls: NewsList</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-pagelist/" rel="bookmark" title="July 20, 2009">EPiServer web controls: PageList</a></li>
<li><a href="http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/" rel="bookmark" title="January 29, 2010">EPiServer code walkthrough #1 &#8211; 404 handler</a></li>
</ul>
<p><!-- Similar Posts took 14.402 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a contact form with ASP.NET MVC</title>
		<link>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=creating-a-contact-form-with-asp-net-mvc</link>
		<comments>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 13 May 2010 15:37:07 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1239</guid>
		<description><![CDATA[We&#8217;re going to create a contact form in ASP.NET MVC 2.0, that uses Ajax to send the form data and that uses client side validation to improve the user experience for our users. Start by creating a new ASP.NET MVC 2.0 web application project in Visual Studio. This will create a new project with the [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re going to create a contact form in ASP.NET MVC 2.0, that uses Ajax to send the form data and that uses client side validation to improve the user experience for our users.</p>
<p>Start by creating a new ASP.NET MVC 2.0 web application project in Visual Studio.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/create-new-aspnet-mvc-application.png" alt="New project dialog in Visual Studio" />
</p>
<p>This will create a new project with the default ASP.NET MVC 2.0 sample code. </p>
<p>Inside the Models folder create a new class, and give it the name: Contact.cs. The class is very simple with just some properties for Name, Email and Comment.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel.DataAnnotations</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ContactForm.<span style="color: #0000FF;">Models</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Contact
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in a name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in an email address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DataType<span style="color: #000000;">&#40;</span>DataType.<span style="color: #0000FF;">EmailAddress</span>, ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Your email address contains some errors&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Email address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Email <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in a comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Comment <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice the Required, DisplayName and DataType attributes. This is called DataAnnotations, and is a new feature in ASP.NET MVC 2.0, which helps us add validation logic to our models.</p>
<p>Next step is creating the /home/contact URL and the contact view. Open up HomeController.cs, and add the following method.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ActionResult Contact<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Place your cursor inside View() and press Ctrl+M, Ctrl+V, or just right-click and choose Add View. Check the &#8220;Create a strongly-typed view&#8221; and type in: ContactForm.Models.Contact.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Title<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span> Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> MasterPageFile<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Views/Shared/Site.Master&quot;</span> Inherits<span style="color: #008000;">=</span><span style="color: #666666;">&quot;System.Web.Mvc.ViewPage&lt;ContactForm.Models.Contact&gt;&quot;</span> <span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Content1&quot;</span> ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;TitleContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
	Contact
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Content2&quot;</span> ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MainContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>h2<span style="color: #008000;">&gt;</span>Contact<span style="color: #008000;">&lt;/</span>h2<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span></pre></div></div>

<p>We can now create the form markup.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Title<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span> Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> MasterPageFile<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Views/Shared/Site.Master&quot;</span> Inherits<span style="color: #008000;">=</span><span style="color: #666666;">&quot;System.Web.Mvc.ViewPage&lt;ContactForm.Models.Contact&gt;&quot;</span> <span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;TitleContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
	Contact us
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MainContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;%</span> Html.<span style="color: #0000FF;">EnableClientValidation</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">%&gt;</span> 
    <span style="color: #008000;">&lt;%</span> <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>Ajax.<span style="color: #0000FF;">BeginForm</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AjaxOptions <span style="color: #000000;">&#123;</span> HttpMethod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Post&quot;</span>, OnComplete <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Contact.onComplete&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationSummary</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;A few fields are still empty&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;</span>fieldset<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>legend<span style="color: #008000;">&gt;</span>Contact us<span style="color: #008000;">&lt;/</span>legend<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;&lt;%:</span> Html.<span style="color: #0000FF;">TextBoxFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">TextBoxFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">TextAreaFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span>, <span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">25</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>input type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submit&quot;</span> value<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Submit&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>fieldset<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>p id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;result&quot;</span><span style="color: #008000;">&gt;&lt;%:</span> TempData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">%&gt;&lt;/</span>p<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;%</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span></pre></div></div>

<p>&lt;%: &#8230; %&gt; is just a shortcut for HTML encoding the data, a new feature in ASP.NET 4.0. &lt;% Html.EnableClientValidation(); %&gt; enables client side validation for us, and must be included right before the start of the form. Ajax.BeginForm is a helper method that generates the form tag for us and attaches a little JavaScript code for sending the form data with Ajax, if the client supports it, otherwise the form data will be sent like normal.</p>
<p>Lets create the JavaScript method that gets called when the form is finished. Create a new JavaScript file inside the Scripts folder and give it the name: Site.js. Add the Contact object with the property function onComplete.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Contact <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>content.<span style="color: #660066;">get_response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> textNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>textNode<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: #339933;">;</span></pre></div></div>

<p>I take the JSON result from the server and add the message to the result paragraph.</p>
<p>If you press F5, the site should open up in your default browser. If you add Home/Contact behind the URL, you should be taken to the Contact Us page.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form.png" alt="Contact us form" />
</p>
<p>For the client side validation to work we need to reference a few JavaScript files. Open up Site.master (located in the shared folder), and add the following code right before the closing body tag.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">...
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftAjax.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftMvcAjax.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftMvcValidation.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/Site.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>You should now receive some friendly error messages when you click the submit button without filling out the form properly.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form-validation.png" alt="Contact us form validation" />
</p>
<p>Next step is adding the action method to HomeController.cs that will handle the form data.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>HttpPost<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Contact<span style="color: #000000;">&#40;</span>Contact model<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;There are a few errors&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ModelState.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Thanks! We'll get back to you soon.&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">IsAjaxRequest</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> JsonResult <span style="color: #000000;">&#123;</span> ContentEncoding <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">UTF8</span>, Data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> success <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, message <span style="color: #008000;">=</span> message <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
        TempData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> message<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice that I check if the request is an Ajax request, if it is I return a JSON object with the result, otherwise I return the whole view with the message.</p>
<p>You would also add some other logic here for saving the message in some way.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form-complete.png" alt="The finished form" />
</p>
<p>That&#8217;s it!<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/10/sending-confirmation-email-to-the-user-when-using-episerver-xforms/" rel="bookmark" title="October 17, 2009">Sending confirmation email to the user when using EPiServer XForms</a></li>
<li><a href="http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/" rel="bookmark" title="July 13, 2009">ASP.NET web forms and jQuery Thickbox plugin</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-property/" rel="bookmark" title="July 5, 2009">EPiServer web controls: Property</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-6-creating-the-xform-page-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 19, 2009">Part 6: Creating the XForm page – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 13.127 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ListControl AppendDataBoundItems and DataBind</title>
		<link>http://www.frederikvig.com/2010/04/listcontrol-appenddatabounditems-and-databind/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=listcontrol-appenddatabounditems-and-databind</link>
		<comments>http://www.frederikvig.com/2010/04/listcontrol-appenddatabounditems-and-databind/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 11:23:51 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1225</guid>
		<description><![CDATA[By default when using code like this, the list item will be cleared when data binding is performed. &#60;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlCategories&#34;&#62; &#60;asp:ListItem&#62;Please choose&#60;/asp:ListItem&#62; &#60;/asp:DropDownList&#62; protected override void OnLoad&#40;EventArgs e&#41; &#123; base.OnLoad&#40;e&#41;; &#160; string&#91;&#93; categories = new&#91;&#93; &#123; &#34;C#&#34;, &#34;ASP.NET&#34;, &#34;EPiServer&#34;, &#34;Umbraco&#34;, &#34;jQuery&#34; &#125;; &#160; ddlCategories.DataSource = categories; ddlCategories.DataBind&#40;&#41;; &#125; Will give us. &#60;select name=&#34;ctl00$MainContent$ddlCategories&#34; id=&#34;MainContent_ddlCategories&#34;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>By default when using code like this, the list item will be cleared when data binding is performed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>DropDownList runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ddlCategories&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ListItem<span style="color: #008000;">&gt;</span>Please choose<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>ListItem<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>DropDownList<span style="color: #008000;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnLoad</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> categories <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;C#&quot;</span>, <span style="color: #666666;">&quot;ASP.NET&quot;</span>, <span style="color: #666666;">&quot;EPiServer&quot;</span>, <span style="color: #666666;">&quot;Umbraco&quot;</span>, <span style="color: #666666;">&quot;jQuery&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	ddlCategories.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> categories<span style="color: #008000;">;</span>
	ddlCategories.<span style="color: #0000FF;">DataBind</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Will give us.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ctl00$MainContent$ddlCategories&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MainContent_ddlCategories&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;C#&quot;</span>&gt;</span>C#<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ASP.NET&quot;</span>&gt;</span>ASP.NET<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;EPiServer&quot;</span>&gt;</span>EPiServer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Umbraco&quot;</span>&gt;</span>Umbraco<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jQuery&quot;</span>&gt;</span>jQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p>As you see &#8220;Please choose&#8221; is not there any more.</p>
<p>However if you set the AppendDataBoundItems property to true (default is false), the list items will be added before data binding is performed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnLoad</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	...
	<span style="color: #0000FF;">ddlCategories</span>.<span style="color: #0000FF;">AppendDataBoundItems</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
	...
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The markup is now correct.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ctl00$MainContent$ddlCategories&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MainContent_ddlCategories&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Please choose&quot;</span>&gt;</span>Please choose<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;C#&quot;</span>&gt;</span>C#<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ASP.NET&quot;</span>&gt;</span>ASP.NET<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;EPiServer&quot;</span>&gt;</span>EPiServer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Umbraco&quot;</span>&gt;</span>Umbraco<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jQuery&quot;</span>&gt;</span>jQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/08/episerver-web-controls-menulist-and-pagetree/" rel="bookmark" title="August 16, 2009">EPiServer web controls: MenuList and PageTree</a></li>
<li><a href="http://www.frederikvig.com/2010/03/starting-out-with-xslt-in-umbraco/" rel="bookmark" title="March 20, 2010">Starting out with XSLT in Umbraco</a></li>
<li><a href="http://www.frederikvig.com/2010/05/episerver-custom-property-with-custom-settings/" rel="bookmark" title="May 14, 2010">EPiServer Custom Property with Custom Settings</a></li>
<li><a href="http://www.frederikvig.com/2010/02/adding-different-css-classes-when-using-the-episerver-pagetree/" rel="bookmark" title="February 28, 2010">Adding different CSS classes when using the EPiServer PageTree control</a></li>
<li><a href="http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/" rel="bookmark" title="April 6, 2010">Detecting Ajax requests on the server</a></li>
</ul>
<p><!-- Similar Posts took 9.753 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/04/listcontrol-appenddatabounditems-and-databind/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detecting Ajax requests on the server</title>
		<link>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=detecting-ajax-requests-on-the-server</link>
		<comments>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 19:27:47 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1222</guid>
		<description><![CDATA[If you use jQuery or ASP.NET AJAX you can easily detect Ajax requests on the server by simply checking for the HTTP_X_REQUESTED_WITH HTTP Header. Both libraries automatically add this to the HTTP Header when they send a request. Here&#8217;s a little code snippet that returns true for Ajax requests. public static bool IsAjaxRequest&#40;&#41; &#123; return [...]]]></description>
			<content:encoded><![CDATA[<p>If you use jQuery or ASP.NET AJAX you can easily detect Ajax requests on the server by simply checking for the HTTP_X_REQUESTED_WITH HTTP Header. Both libraries automatically add this to the HTTP Header when they send a request.</p>
<p>Here&#8217;s a little code snippet that returns true for Ajax requests.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> IsAjaxRequest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;X-Requested-With&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;X-Requested-With&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;XMLHttpRequest&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The cool thing about this is that you can use this in a base page or a HTTP Module, and customize the returned data. You could for instance return JSON or XML.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> MyWebApplication
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BasePage <span style="color: #008000;">:</span> Page
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnInit<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnInit</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Helper.<span style="color: #0000FF;">IsAjaxRequest</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Response.<span style="color: #0000FF;">Headers</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/json&quot;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>json output<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/" rel="bookmark" title="May 13, 2010">Creating a contact form with ASP.NET MVC</a></li>
<li><a href="http://www.frederikvig.com/2010/02/getting-the-page-and-episerver-currentpage-object-from-httpcontext/" rel="bookmark" title="February 20, 2010">Getting the Page and EPiServer CurrentPage object from HttpContext</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/09/creating-a-custom-episerver-paging-control/" rel="bookmark" title="September 20, 2009">Creating a Custom EPiServer Paging Control</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 13.461 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the Page and EPiServer CurrentPage object from HttpContext</title>
		<link>http://www.frederikvig.com/2010/02/getting-the-page-and-episerver-currentpage-object-from-httpcontext/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=getting-the-page-and-episerver-currentpage-object-from-httpcontext</link>
		<comments>http://www.frederikvig.com/2010/02/getting-the-page-and-episerver-currentpage-object-from-httpcontext/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 10:25:37 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[EPiServer]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1092</guid>
		<description><![CDATA[Just a little quick tip when needing to use either the Page object or the EPiServer CurrentPage object from a class file. HttpContext.Current will give you access to the current request, what we can do is cast HttpContext.Current.Handler (since Page implements the IHttpHandler interface) to System.Web.UI.Page. var page = HttpContext.Current.Handler as System.Web.UI.Page; &#160; if &#40;page [...]]]></description>
			<content:encoded><![CDATA[<p>Just a little quick tip when needing to use either the Page object or the EPiServer CurrentPage object from a class file. HttpContext.Current will give you access to the current request, what we can do is cast HttpContext.Current.Handler (since Page implements the IHttpHandler interface) to System.Web.UI.Page.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var page <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Handler</span> <span style="color: #0600FF;">as</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span></span>.<span style="color: #0000FF;">Page</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>page <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// do something with the page object</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We can take this a step further and cast it to EPiServer.PageBase which gives us access to the CurrentPage object.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> PageData CurrentPage
<span style="color: #000000;">&#123;</span>
    get
    <span style="color: #000000;">&#123;</span>
	var page <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Handler</span> <span style="color: #0600FF;">as</span> EPiServer.<span style="color: #0000FF;">PageBase</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>page <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	    <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> page.<span style="color: #0000FF;">CurrentPage</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/" rel="bookmark" title="April 6, 2010">Detecting Ajax requests on the server</a></li>
<li><a href="http://www.frederikvig.com/2009/12/extending-pagedata-with-some-cool-html-helpers/" rel="bookmark" title="December 8, 2009">Extending PageData with some cool Html Helpers</a></li>
<li><a href="http://www.frederikvig.com/2009/05/episerver-extension-methods/" rel="bookmark" title="May 4, 2009">EPiServer Extension Methods</a></li>
<li><a href="http://www.frederikvig.com/2009/10/sending-confirmation-email-to-the-user-when-using-episerver-xforms/" rel="bookmark" title="October 17, 2009">Sending confirmation email to the user when using EPiServer XForms</a></li>
<li><a href="http://www.frederikvig.com/2009/09/creating-a-custom-episerver-paging-control/" rel="bookmark" title="September 20, 2009">Creating a Custom EPiServer Paging Control</a></li>
</ul>
<p><!-- Similar Posts took 13.307 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/02/getting-the-page-and-episerver-currentpage-object-from-httpcontext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2010 EPiServer Snippets</title>
		<link>http://www.frederikvig.com/2010/02/visual-studio-2010-episerver-snippets/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=visual-studio-2010-episerver-snippets</link>
		<comments>http://www.frederikvig.com/2010/02/visual-studio-2010-episerver-snippets/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:38:06 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1079</guid>
		<description><![CDATA[I finally got my hands on a copy of Visual Studio 2010 RC1! After playing around a bit, I stumbled across the new snippet functionality in Visual Studio 2010. You can now use snippets in the markup files as well (in previous versions you could only use the snippet functionality in code files like class/interfaces/code-behind [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got my hands on a copy of Visual Studio 2010 RC1! After playing around a bit, I stumbled across the new snippet functionality in Visual Studio 2010. You can now use snippets in the markup files as well (in previous versions you could only use the snippet functionality in code files like class/interfaces/code-behind files etc). This is very cool, and Microsoft has even included a few snippets for their ASP.NET controls and for HTML elements like: a, table, img, div etc. Quite a time saver! </p>
<p>To test the snippets out, simply type the shortcut (eg. &#8216;a&#8217; or &#8216;table&#8217;) and press tab. </p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/VS2010/table.png" alt="Visual Studio 2010 table snippet" /></p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/VS2010/table-after-tab.png" alt="Visual Studio 2010 table snippet" /></p>
<p>This is a simple, but very cool feature. Imaging all the typing you can get rid off! </p>
<h3>Creating your own snippets</h3>
<p>In Visual Studio 2010, under the Tools menu, you&#8217;ll find the Code Snippets Manager (ctrl+k, ctrl+b).  </p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/VS2010/snippets-manager.png" alt="Visual Studio 2010 Code Snippets Manager" /></p>
<p>Here you can add new folders that contain your custom snippets, or check out the other snippets added by Microsoft. You&#8217;ll also see the path to the snippets folder (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\ in my case). If you open up that folder in Windows Explorer you should see at least two folders there, ASP.NET and HTML. Inside both of these folders you&#8217;ll find the snippets for the ASP.NET web controls and for HTML elements. We can open up one of the files and take a look at the code.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CodeSnippet</span> <span style="color: #000066;">Format</span>=<span style="color: #ff0000;">&quot;1.1.0&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>image<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Microsoft Corporation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>image<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;AlternativeShortcuts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>imagebutton<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Shortcut</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;image&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>asp:image<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Shortcut</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;imagebutton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>asp:imagebutton<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/AlternativeShortcuts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Markup snippet for a control that contains an image<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SnippetTypes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Expansion<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SnippetTypes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ID<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>imageurl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ID<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ToolTip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>imageurl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ToolTip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>imageurl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Code</span> <span style="color: #000066;">Language</span>=<span style="color: #ff0000;">&quot;html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #339933;">&lt;![CDATA[&lt;asp:$shortcut$ imageurl=&quot;$imageurl$&quot; runat=&quot;server&quot; /&gt;$end$]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CodeSnippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Very easy and readable XML markup, that we easily can tweak to our needs. Say we&#8217;re working on a project where 90% of the tables we create should have a class of &#8220;products&#8221;. Lets create a snippet for this, so that we don&#8217;t have to type the same thing every time. </p>
<p>Create a new snippet called table-products.snippet, open it up in your favorite code editor and add this code.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CodeSnippet</span> <span style="color: #000066;">Format</span>=<span style="color: #ff0000;">&quot;1.1.0&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>table<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Frederik Vig<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tablep<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Shortcut<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Markup snippet for a table with class products<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SnippetTypes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Expansion<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SurroundsWith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SnippetType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SnippetTypes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Header<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ID<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cellspacing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ID<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ToolTip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cellspacing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ToolTip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Code</span> <span style="color: #000066;">Language</span>=<span style="color: #ff0000;">&quot;html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #339933;">&lt;![CDATA[&lt;table cellspacing=&quot;$cellspacing$&quot; class=&quot;products&quot;&gt;</span>
<span style="color: #339933;">    &lt;tr&gt;</span>
<span style="color: #339933;">        &lt;td&gt;$selected$$end$&lt;/td&gt;</span>
<span style="color: #339933;">    &lt;/tr&gt;</span>
<span style="color: #339933;">&lt;/table&gt;]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Code<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Snippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CodeSnippet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I&#8217;ve saved table-products.snippet directly in the HTML snippet folder (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\HTML in my case), but you can save it anywhere, just remember to add the snippet folder with the Code Snippets Manager in Visual Studio. </p>
<p>We can now test the snippet by typing tablep and pressing tab.</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/VS2010/tablep.png" alt="Visual Studio 2010 table products snippet" /></p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/VS2010/tablep-after-tab.png" alt="Visual Studio 2010 table products snippet" />
</p>
<p>Simple example, I know, but you get the idea!.</p>
<h3>EPiServer</h3>
<p>Naturally, one of the first things I did was add the <a href="http://www.frederikvig.com/2009/07/episerver-web-controls-property/">EPiServer Property web control</a> to the snippet manager <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . This turned out to be very easy! So I continued on adding snippets for the other EPiServer web controls as well. Here is the list of snippets with their shortcut.</p>
<ul>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_Calendar.htm">EPiServer Calendar</a> &#8211; shortcut: ec</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_CategoryDataSource.htm">EPiServer CategoryDataSource</a> &#8211; shortcut: ecd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_FileSystemDataSource.htm">EPiServer FileSystemDataSource</a> &#8211; shortcut: efsd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_LanguageDataSource.htm">EPiServer LanguageDataSource</a> &#8211; shortcut: eld</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_MembershipUserDataSource.htm">EPiServer MembershipUserDataSource</a> &#8211; shortcut: emud</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_MenuList.htm">EPiServer MenuList</a> &#8211; shortcut: eml</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_NewsList.htm">EPiServer NewsList</a> &#8211; shortcut: enl</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_OnPageEditControl.htm">EPiServer OnPageEditControl</a> &#8211; shortcut: eopec</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageDataSource.htm">EPiServer PageDataSource</a> &#8211; shortcut: epd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageDefinitionDataSource.htm">EPiServer PageDefinitionDataSource</a> &#8211; shortcut: epdd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageDefinitionTypeDataSource.htm">EPiServer PageDefinitionTypeDataSource</a> &#8211; shortcut: epdtd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageList.htm">EPiServer PageList</a> &#8211; shortcut: epl</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageListData.htm">EPiServer PageListData</a> &#8211; shortcut: epld</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageTree.htm">EPiServer PageTree</a> &#8211; shortcut: ept</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageTreeData.htm">EPiServer PageTreeData</a> &#8211; shortcut: eptd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PageVersionDataSource.htm">EPiServer PageVersionDataSource</a> &#8211; shortcut: epvd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_Property.htm">EPiServer Property</a> &#8211; shortcut: ep</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_PropertyCriteriaControl.htm">EPiServer PropertyCriteriaControl</a> &#8211; shortcut: epcc</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_SearchDataSource.htm">EPiServer SearchDataSource</a> &#8211; shortcut: esd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_SoftLinkDataSource.htm">EPiServer SoftLinkDataSource</a> &#8211; shortcut: esld</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_SubscriptionDataSource.htm">EPiServer SubscriptionDataSource</a> &#8211; shortcut: esds</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_TabDefinitionDataSource.htm">EPiServer TabDefinitionDataSource</a> &#8211; shortcut: etdd</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_ThemeImage.htm">EPiServer ThemeImage</a> &#8211; shortcut: eti</li>
<li><a href="http://sdk.episerver.com/library/cms6/html/T_EPiServer_Web_WebControls_Translate.htm">EPiServer Translate</a> &#8211; shortcut: et</li>
</ul>
<p><a href="http://www.frederikvig.com/wp-content/uploads/VS2010/EPiServer-VS2010-Snippets.zip">Download the snippets</a></p>
<p> <strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2010/03/starting-out-with-xslt-in-umbraco/" rel="bookmark" title="March 20, 2010">Starting out with XSLT in Umbraco</a></li>
<li><a href="http://www.frederikvig.com/2010/04/creating-a-simple-image-gallery-with-episerver/" rel="bookmark" title="April 3, 2010">Creating a simple image gallery with EPiServer</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-1-setting-up-the-development-environment-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 4, 2009">Part 1: Setting up the development environment &#8211; Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/" rel="bookmark" title="May 13, 2010">Creating a contact form with ASP.NET MVC</a></li>
<li><a href="http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/" rel="bookmark" title="January 29, 2010">EPiServer code walkthrough #1 &#8211; 404 handler</a></li>
</ul>
<p><!-- Similar Posts took 13.688 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/02/visual-studio-2010-episerver-snippets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EPiServer code walkthrough #1 &#8211; 404 handler</title>
		<link>http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=episerver-code-walkthrough-1-404-handler</link>
		<comments>http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:10:11 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[EPiServer code walkthrough]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1038</guid>
		<description><![CDATA[This is the first post in a new series called &#8220;EPiServer code walkthrough&#8221;. What I&#8217;ll do is go through one new EPiServer module in each post. Writing a little about what it does, learn by reading its code, and hopefully contributing a little back to the project, if I see any bugs or harmful/unnecessary code [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post in a new series called &#8220;EPiServer code walkthrough&#8221;. What I&#8217;ll do is go through one new EPiServer module in each post. Writing a little about what it does, learn by reading its code, and  hopefully contributing a little back to the project, if I see any bugs or harmful/unnecessary code that is <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>I&#8217;m a firm believer that reading code helps you become a better developer. There are lots of modules even I just recently came across, that I didn&#8217;t know existed just a few weeks back. I think I&#8217;m not the only one, especially since they are spread around on various blogs, <a href="http://www.codeplex.com/">CodePlex</a>, <a href="https://www.coderesort.com/p/epicode/wiki/WikiStart">EPiCode</a> and other places. They all contain valuable code that we as EPiServer developers should read.</p>
<p>I&#8217;m starting with a module I personally have not used much before, but that I know a lot of people have used in their projects: the <a href="https://www.coderesort.com/p/epicode/wiki/404Handler">custom 404 handler</a>. </p>
<blockquote><p>
<cite>From the project wiki page</cite></p>
<p>This module has a more advanced url redirect feature than the built-in shortcut url feature in EPiServer. It handles extensions and querystring parameters too. If you have a lot of 404 errors in your logs, you can use this to redirect the user to the correct page. Especially useful if you move templates or pages around, or have just installed EPiServer and have a lot of old urls that is no longer available.</p>
</blockquote>
<p><a href="https://www.coderesort.com/p/epicode/wiki/404Handler">404 handlers wiki page</a> explains in detail what you need to do to install the module, so I&#8217;m not going to repeat it here. Instead we&#8217;ll take a look at the code. </p>
<p>Start by doing a SVN checkout of the source code (the svn url is https://www.coderesort.com/svn/epicode/BVNetwork.404Handler/5.x/).</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/EPiServer-code-walkthrough/404handler/checkout.png" alt="SVN checkout of 404 handler" /></p>
<p>When opening up the BVNetwork.404Handler folder you&#8217;ll see a readme.txt file explaining the purpose of the module, installation, configuration etc. This is something I like! Not everyone reads the wiki page, and we as developers are especially lazy when it comes to reading documentation. Having a readme.txt with everything you need makes this so much easier, and gives the module author less support questions to answer. When using a new module, always make sure to read the documentation before asking the author questions. Saves both parties time <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Lets open up BVNetwork.404Handler.csproj in Visual Studio.</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/EPiServer-code-walkthrough/404handler/visual-studio-project-structure.png" alt="404 handler project structure in Visual Studio" /></p>
<p>As you can see the project is structured pretty nice, with good separations between the different parts. Very good that the project uses languages files, and that they&#8217;ve included them for five different languages. Not all modules use the language files, and if they do, they seldom have any more than one update-to-date English language file. Kudos for this. </p>
<p>What I don&#8217;t like is the missing fnf_logo.gif image file in the Images folder. A quick search in Visual Studio shows that this file is no longer in use, and should therefore be removed from the project. A missing image file is not the end of the world, and I know people make mistakes/forget to commit all their files, but this can be very annoying and even lead to a lot of work for other developers using your code or taking over a project. </p>
<p>Always make sure that your project builds on other computers than your own. I recommend manually setting up the project on preferably a new computer to see that everything works as expected, you can also use the same computer, but make sure to do the normal process a new developer would use when setting up the project (new checkout etc). Especially on larger projects that have quite a few things to setup you should do this. You&#8217;ll also receive some valuable feedback if your setup process is to complex (again on larger projects this happens quite frequently). Another thing you should use is a <a href="http://en.wikipedia.org/wiki/Continuous_integration">build server</a>. There are <a href="http://www.jetbrains.com/teamcity/">quite</a> a <a href="http://cruisecontrol.sourceforge.net/">few</a> out there, I know CodeResort just added <a href="http://bitten.edgewall.org/">Bitten</a> as their build server for CodeResort projects. In a nutshell what a build server does is make sure you&#8217;ve included all the files and resources needed to build your project, run various tests (if you have any), deploy your files, create reports, notify team members if their build fails, and a bunch of other stuff, automatically for you. I&#8217;ll try to write a blog post up with more information on build and continuous integration servers.</p>
<p>Lets get back to the Visual Studio and the project! One of the things you should start out with is actually making sure the project builds after doing a checkout. So, either press ctrl + shift + b or go to Build -> Build BVNetwork.404Handler. You should have a successful build.</p>
<p>If we take a look through the code we see that most of the code is well documented and follows a nice naming convention making it easy to know what the purpose of the class is (CustomRedirectHandler.cs, CustomRedirectCollection.cs, Custom404Handler.cs etc). But we also see a few minor things we can clean up: unused using statements, commented out code, unused field variables etc. These are minor, but still important things to get rid of. Always strive for a clean code base, commented out code is something you should never commit into a repository, the whole reason for having a source control system is so that you can look at previous changesets and their code, there is no reason to keep the commented out code there, it will only be forgotten by the developer who commented out the code, and the other developers don&#8217;t know what to do with it, and will most likely just let it be. When I come across this I show no mercy and just delete it! </p>
<p>You&#8217;ll also see a few System.Diagnostics.Debug.WriteLine that are commented out. The project now uses Log4Net to log errors and warnings, System.Diagnostics.Debug.WriteLine is legacy code that we can safely remove. Another thing that I prefer is using string.Empty(); instead of &#8220;&#8221;, I&#8217;ve not updated the code because I feel this is more a personal preference. Same thing when it comes to over-documenting-code, like in this example.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// The refering url</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> GetReferer<span style="color: #000000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span></span>.<span style="color: #0000FF;">Page</span> page<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">string</span> referer <span style="color: #008000;">=</span> page.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">ServerVariables</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;HTTP_REFERER&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>referer <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Strip away host name in front, if local redirect</span>
	<span style="color: #FF0000;">string</span> hostUrl <span style="color: #008000;">=</span> EPiServer.<span style="color: #0000FF;">Configuration</span>.<span style="color: #0000FF;">Settings</span>.<span style="color: #0000FF;">Instance</span>.<span style="color: #0000FF;">SiteUrl</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>referer.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>hostUrl<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	    referer <span style="color: #008000;">=</span> referer.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, hostUrl.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
	referer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Can't have null</span>
    <span style="color: #0600FF;">return</span> referer<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The document header can safely be removed since the method name does a good job at describing what the purpose of the method is. I&#8217;ve not removed the document header, since again this is my personal preference.</p>
<p>A thing I just noticed is that this is actually a Visual Studio class project, and not a Web Application project. I can only guess, but this is probably a project that got upgraded from Visual Studio 2003, I&#8217;ve previously had some problem upgrading a project from Visual Studio 2003 to Visual Studio 2008. The conversion usually goes okay (just an update to the csproj file mostly), but the ProjectTypeGuids doesn&#8217;t always get added. This is easy to fix, open up BVNetwork.404Handler.csproj in notepad or another source editor, and add this line.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ProjectTypeGuids<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ProjectTypeGuids<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Save, and open up the project in Visual Studio. You should now see a slightly different icon at the top of the solution explorer window.</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/EPiServer-code-walkthrough/404handler/new-icon.png" alt="New project icon in Visual Studio" /></p>
<p>This will allow us to add a designer file to default.aspx from the admin folder. To do this simply right-click the file in solution explorer and choose &#8220;Convert to Web Application&#8221;. We can now delete the &#8220;Web Form Designer generated code&#8221;-region.</p>
<p>After all these updates its very important to make sure that the project still builds and that everything functions as expected by testing it.</p>
<p>What I liked the most with this project is how it uses logging. I&#8217;m personally very bad at using logging, and know that not all projects use logging as much as they should. Kudos to the authors for this!</p>
<p>I hope you&#8217;ve learned a few new things while reading this code, and be sure to try out the 404 handler! For SEO and user experience purposes it is crucial that the users have a smooth transition. They shouldn&#8217;t even notice the change when switching to EPiServer from another CMS (that uses different urls). A 404 (file not found) error message can also quickly become a major leak of traffic for your clients site.<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/12/part-1-setting-up-the-development-environment-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 4, 2009">Part 1: Setting up the development environment &#8211; Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/01/2010/" rel="bookmark" title="January 9, 2010">2010</a></li>
<li><a href="http://www.frederikvig.com/2010/02/visual-studio-2010-episerver-snippets/" rel="bookmark" title="February 11, 2010">Visual Studio 2010 EPiServer Snippets</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/04/creating-a-simple-image-gallery-with-episerver/" rel="bookmark" title="April 3, 2010">Creating a simple image gallery with EPiServer</a></li>
</ul>
<p><!-- Similar Posts took 14.195 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Html Helpers vs Server controls</title>
		<link>http://www.frederikvig.com/2009/12/html-helpers-vs-server-controls/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=html-helpers-vs-server-controls</link>
		<comments>http://www.frederikvig.com/2009/12/html-helpers-vs-server-controls/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 18:24:32 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=732</guid>
		<description><![CDATA[I received some very good comments on my post Extending PageData with some cool Html Helpers. Which made me want to explain my thoughts on the subject a bit more . For me, most of the server controls abstract away to much of the details, that I feel we as web developers should be comfortable [...]]]></description>
			<content:encoded><![CDATA[<p>I received some <a href="http://www.frederikvig.com/2009/12/extending-pagedata-with-some-cool-html-helpers/comment-page-1/#comment-94">very good comments</a> on my post <a href="http://www.frederikvig.com/2009/12/extending-pagedata-with-some-cool-html-helpers/">Extending PageData with some cool Html Helpers</a>. Which made me want to explain my thoughts on the subject a bit more <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>For me, most of the server controls abstract away to much of the details, that I feel we as web developers should be comfortable with, and want to have full control over. Server controls make use of viewstate, affecting both performance and increasing the size of the page. Many render old deprecated HTML markup, make it hard to work with ids in CSS and JavaScript, and have a complex life cycle. </p>
<p>I know there are ways to fix some of these issues (turn off viewstate, control adapters etc). But I feel that the added work load when using server controls, and trying to fix them, is higher than the benefit we get. With Html Helper, I type so much less code than before, and I have full control. I can easily add my own extension methods for stuff that I use a lot, I&#8217;ve one place to go to update the code. Combined with Page Type Builder I have intellisense of all my code, I can refactor the code without worrying about breaking it. Instead of having to check for runtime errors I can compile my .aspx/.ascx and be sure that I haven&#8217;t added a typo. </p>
<h3>A little comparison</h3>
<p>Lets say we are tasked with implementing this simple list.</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/news-list.png" alt="" /></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>Recent Articles<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;block odd&quot;</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/thumb-1.jpg&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;240px&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100px&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blk-top&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h4</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;</span>Aliquam Risus Justo Lorem Ipsum Dolor Sit Amet<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h4</span>&gt;</span>	
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;datetime&quot;</span>&gt;</span>August 27, 2009<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;comment&quot;</span>&gt;</span>2 Comments<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>		
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>						
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blk-content&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
		Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec libero. Suspendisse bibendum. 
		Cras id urna. Morbi tincidunt, orci ac convallis aliquam, lectus turpis varius lorem, eu 
		posuere nunc justo tempus leo. Donec mattis, purus nec placerat bibendum, dui pede condimentum 
		odio, ac blandit ante orci ut diam. Cras fringilla magna. Phasellus suscipit, leo a pharetra 
		condimentum, lorem tellus eleifend magna, eget fringilla velit magna id neque. 				
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>					
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;more&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;</span>continue reading <span style="color: #ddbb00;">&amp;raquo;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</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;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;block even&quot;</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/thumb-2.jpg&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thumbnail&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;240px&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100px&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blk-top&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h4</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;</span>Aliquam Risus Justo<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h4</span>&gt;</span>	
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;datetime&quot;</span>&gt;</span>August 26, 2009<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;comment&quot;</span>&gt;</span>2 Comments<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>						
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blk-content&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
		Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec libero. Suspendisse bibendum. 
		Cras id urna. Morbi tincidunt, orci ac convallis aliquam, lectus turpis varius lorem, eu 
		posuere nunc justo tempus leo. Donec mattis, purus nec placerat bibendum, dui pede condimentum 
		odio, ac blandit ante orci ut diam. Cras fringilla magna. Phasellus suscipit, leo a pharetra 
		condimentum, lorem tellus eleifend magna, eget fringilla velit magna id neque. 	
&nbsp;
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;more&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.html&quot;</span>&gt;</span>continue reading <span style="color: #ddbb00;">&amp;raquo;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>			
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fix&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
...</pre></div></div>

<p>We might implement this by using a Repeater control, and a user control with the news story.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>h3<span style="color: #008000;">&gt;</span>Recent Articles<span style="color: #008000;">&lt;/</span>h3<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Repeater runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;rptNewsList&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>ItemTemplate<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;block odd&quot;</span><span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>Jungle<span style="color: #008000;">:</span>NewsStory runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> NewsPage<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%# Container.DataItem %&gt;&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>ItemTemplate<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>AlternatingItemTemplate<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;block even&quot;</span><span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>Jungle<span style="color: #008000;">:</span>NewsStory runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> NewsPage<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%# Container.DataItem %&gt;&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;fix&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>AlternatingItemTemplate<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Repeater<span style="color: #008000;">&gt;</span></pre></div></div>

<p>The NewsStory user control might look like this when using Html helper methods.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>a href<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%= NewsPage.LinkURL %&gt;&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;%=</span> NewsPage.<span style="color: #0000FF;">HtmlImage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ListImage&quot;</span>, <span style="color: #666666;">&quot;PageName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;/</span>a<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;blk-top&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>h4<span style="color: #008000;">&gt;&lt;%=</span> NewsPage.<span style="color: #0000FF;">HtmlLink</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;&lt;/</span>h4<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;&lt;</span>span <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;datetime&quot;</span><span style="color: #008000;">&gt;&lt;%=</span> NewsPage.<span style="color: #0000FF;">StartPublish</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;dd MMMM yyyy&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;&lt;/</span>span<span style="color: #008000;">&gt;&lt;/</span>p<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;blk-content&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;&lt;%=</span> NewsPage.<span style="color: #0000FF;">PreviewText</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">200</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;</span>a <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;more&quot;</span> href<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%= NewsPage.LinkURL %&gt;&quot;</span><span style="color: #008000;">&gt;</span><span style="color: #0600FF;">continue</span> reading <span style="color: #008000;">&amp;</span>raquo<span style="color: #008000;">;&lt;/</span>a<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span></pre></div></div>

<p>For me this code is very easy to change and update. It is very readable (for me HTML markup describe the meaning of the content and give it context). I can easily add ids where I need. Update the markup without having to change my code-behind (and compile). If I need to change some of the logic I have one extension method to update, not a bunch of code-behind files to go to.</p>
<p>By using server controls it might look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>HyperLink runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;lnkListImage&quot;</span> <span style="color: #008000;">/&gt;</span>
<span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;blk-top&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>h4<span style="color: #008000;">&gt;&lt;</span>EPiServer<span style="color: #008000;">:</span>Property runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> PropertyName<span style="color: #008000;">=</span><span style="color: #666666;">&quot;PageLink&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;heading&quot;</span> <span style="color: #008000;">/&gt;&lt;/</span>h4<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;&lt;</span>asp<span style="color: #008000;">:</span>Label runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;datetime&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;lblDate&quot;</span> <span style="color: #008000;">/&gt;&lt;/</span>p<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;blk-content&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;&lt;</span>asp<span style="color: #008000;">:</span>Literal runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ltlPreviewText&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>HyperLink runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;lnkReadMore&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;more&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;continue reading &amp;raquo;&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span></pre></div></div>

<p>And the code-behind.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnLoad</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lnkListImage</span>.<span style="color: #0000FF;">ImageUrl</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ListImage&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> <span style="color: #FF0000;">string</span> <span style="color: #008000;">??</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lnkListImage</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>.<span style="color: #0000FF;">PageName</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lnkListImage</span>.<span style="color: #0000FF;">NavigateUrl</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>.<span style="color: #0000FF;">LinkURL</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">heading</span>.<span style="color: #0000FF;">PageLink</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>.<span style="color: #0000FF;">PageLink</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lblDate</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>.<span style="color: #0000FF;">StartPublish</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;dd MMMM yyyy&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ltlPreviewText</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> Helper.<span style="color: #0000FF;">PreviewText</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>, <span style="color: #FF0000;">200</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lnkReadMore</span>.<span style="color: #0000FF;">NavigateUrl</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">NewsPage</span>.<span style="color: #0000FF;">LinkURL</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>They both do the same thing, and at this point it really is just about what you prefer. But, (for me at least) those extra lines of repetitive code get quite boring to type after a while. There are of course possibilities to have a bunch of helper classes that generate these server controls for you, and then just add them to the various control collections. But why go to all that problem, when a simple extension method in most cases does a much better job? </p>
<p>What do I use? Both. I use what makes the most sense there and then. Server controls are here to help me as a developer, same with extension methods, they are here to help me. When I come to the point that I have to use a lot of hacks and fixes to make something work, I stop, take another look at my solution, and try to use a different approach instead. I feel we&#8217;ve reached a point where we need to fix/hack to much to make server controls work. That might be why my most used server control is the Repeater and recently the ListView <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/12/part-3-creating-the-start-page-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 10, 2009">Part 3: Creating the start page – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-2-creating-a-foundation-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 7, 2009">Part 2: Creating a foundation &#8211; Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/12/extending-pagedata-with-some-cool-html-helpers/" rel="bookmark" title="December 8, 2009">Extending PageData with some cool Html Helpers</a></li>
<li><a href="http://www.frederikvig.com/2009/11/the-selectedtemplate-and-duplicate-code/" rel="bookmark" title="November 11, 2009">The SelectedTemplate and duplicate code</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-pagelist/" rel="bookmark" title="July 20, 2009">EPiServer web controls: PageList</a></li>
</ul>
<p><!-- Similar Posts took 17.785 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/12/html-helpers-vs-server-controls/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating a mobile version of a web site</title>
		<link>http://www.frederikvig.com/2009/10/creating-a-mobile-version-of-a-web-site/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=creating-a-mobile-version-of-a-web-site</link>
		<comments>http://www.frederikvig.com/2009/10/creating-a-mobile-version-of-a-web-site/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:51:31 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=244</guid>
		<description><![CDATA[When building web sites we has developers or designers have to take into consideration all the different types of devices that can be used to access the web sites we create. Not just PC or Mac with Internet Explorer, Firefox, Safari, Opera, Jaws, or any other browser. But also mobile devices, like iPhones, iTouch, Nintendo [...]]]></description>
			<content:encoded><![CDATA[<p>When building web sites we has developers or designers have to take into consideration all the different types of devices that can be used to access the web sites we create. Not just PC or Mac with Internet Explorer, Firefox, Safari, Opera, Jaws, or any other browser. But also mobile devices, like iPhones, iTouch, Nintendo Wii. </p>
<p>Especially in recent years, after the launch of the iPhone, accessing online information through a mobile device has become more common. This is something that is going to increase even more in the next few years.</p>
<p>Below I&#8217;ve listed some resources for creating a mobile version of a web site that I hope you&#8217;ll find useful.</p>
<h3>Mobify</h3>
<p><a href="http://mobify.me/">Mobify</a> is a free online service that lets you convert an existing web site into a mobile version. This is done by selecting the content you wish to display for mobile users, customizing it by changing the display order and tweaking the CSS. The last step is to publish it by creating an url and adding a little JavaScript to your page. </p>
<p>Check out <a href="http://woork.blogspot.com/2009/08/how-to-implement-mobile-version-of-your.html">How to implement a mobile version of your blog in three simple steps</a> for a quick introduction.</p>
<p>Mobify is quick and easy way to great a mobile version, but you cannot change the HTML code and you&#8217;re stuck with the options the service offers. It can sometimes be better to create a mobile version from scratch.</p>
<h4>Update 05.10.2009</h4>
<p>Igor Faletski, founder of <a href="http://www.mobify.me">Mobify</a>, was kind enough to send me an email explaining the use of HTML blocks to add more static content. While this is a great feature I still miss the ability to update my existing markup and easily see what classes and ids that the mobile version uses. Now I have to test and tweak a little before I get the right id or class when changing my CSS.</p>
<h3>Creating a mobile version with ASP.NET</h3>
<p>Although services like Mobify are great, sometimes you need full control. On <a href="http://www.mobify.me/">mobify.me</a> it says that Mobify supports over 4500+ different mobile devices. This is a lot of different devices to support! Fortunately there is an open source project on CodePlex that has done most of the work for us. It is called <a href="http://mdbf.codeplex.com/">Mobile Device Browser File</a> and consists of a browser file with information about what the different mobile devices support (Java, JavaScript, Images, Flash etc). This project is pretty active with new devices added all the time. </p>
<p>You simply download the browser file and add it to your projects App_Browser folder. After you&#8217;ve done that you&#8217;ll be able to use Request.Browser["IsMobileDevice"] to detect if the device accessing your page is actually a mobile device. There are also <a href="http://mdbf.codeplex.com/Wiki/View.aspx?title=Capabilities&#038;referringTitle=Home">64 other capabilities</a> that offers a lot of different detection functionality. You can for instance check if the device supports Flash, JavaScript, Color, CSS background images etc.</p>
<p>If you&#8217;re using ASP.NET MVC check out <a href="http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx">Scott Hanselman&#8217;s post about how he implemented this with Nerd Dinner</a>. </p>
<p>If you&#8217;re using ASP.NET web forms there are a couple of approaches you could take. You could use a different master page for the mobile version by adding some detection code to your base page and <a href="http://odetocode.com/Articles/450.aspx">switching the master pages in the Init method</a>. Or you could register a <a href="http://msdn.microsoft.com/en-us/library/ms227673.aspx">custom HttpModule</a> that redirects the user to a different page if it is a mobile device.</p>
<p>Recently I stumbled over another library to detect the mobile device, it is called <a href="http://www.51degrees.mobi/Products/NETMobileAPI">51Degrees.mobi</a>. <a href="http://dotnetslackers.com/articles/aspnet/Mobile-Device-Detection-and-Redirection-Using-ASP-NET.aspx">Here is the article</a> that describes how to use it.</p>
<h3>iPhone</h3>
<p>Apple iPhone uses the Safari browser as its default browser. The great thing about that is that CSS3 stuff like <a href="http://www.css3.info/preview/rounded-border.html">border-radius</a>, multiple background images, <a href="http://furbo.org/2007/07/11/quartz-and-JavaScript-sitting-in-a-tree/">canvas</a> and more are supported. </p>
<p>If you like to simply target iPhone users you can do so by simply adding a custom stylesheet with this code.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;only screen and (max-device-width: 480px)&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;iPhone.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>For more information I recommend reading <a href="http://www.alistapart.com/articles/putyourcontentinmypocket/">A List Aparts Put Your Content in My Pocket</a> and checking out the <a href="http://developer.apple.com/iphone/">iPhone Dev Center</a>.</p>
<h3>WordPress</h3>
<p>For WordPress you have a nice plugin called <a href="http://www.bravenewcode.com/wptouch/">WPtouch</a>, that will help you easily add a mobile view for iPhone, iPod, Android, Storm and Pre. You just need to install and activate the plugin, and you&#8217;re ready! You can easily customize it to your needs as well. </p>
<h3>Other resources</h3>
<p>More and more resources are being added each day. Below are some I&#8217;ve bookmarked and that have helped me in the past.</p>
<ul>
<li><a href="http://quirksmode.org/m/">Quirksmode great set of mobile tests</li>
<li><a href="http://mobify.me/">Mobify easily turn an existing site into a mobile view</a></li>
<li><a href="http://mobilewebbook.com/">Cameron Molls book about mobile web design</a></li>
<li><a href="http://mdbf.codeplex.com/">Mobile Device Browser File</a></li>
<li><a href="http://www.w3.org/Mobile/">W3C Mobile Web Initiative</a></li>
<li><a href="http://www.w3.org/TR/mwabp/">W3C Mobile Web Application Best Practices</a></li>
<li><a href="http://ready.mobi/launch.jsp?locale=en_EN">Test your website for mobile compatibility</a></li>
</ul>
<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/" rel="bookmark" title="July 13, 2009">ASP.NET web forms and jQuery Thickbox plugin</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-1-setting-up-the-development-environment-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 4, 2009">Part 1: Setting up the development environment &#8211; Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/12/introduction-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 1, 2009">Introduction &#8211; Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/04/creating-a-simple-image-gallery-with-episerver/" rel="bookmark" title="April 3, 2010">Creating a simple image gallery with EPiServer</a></li>
</ul>
<p><!-- Similar Posts took 13.263 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/10/creating-a-mobile-version-of-a-web-site/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Extending search field with suggestion box</title>
		<link>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=extending-search-field-with-suggestion-box</link>
		<comments>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:20:58 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=212</guid>
		<description><![CDATA[Disclaimer: In this example I&#8217;ve kept the code simple to make it easier to read and to give you an idea of how you might approach something like this with EPiServer. This code should not be used in production scenarios since it will use a lot of resources. Use at own risk . Update &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: In this example I&#8217;ve kept the code simple to make it easier to read and to give you an idea of how you might approach something like this with EPiServer. This code should not be used in production scenarios since it will use a lot of resources. Use at own risk <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Update &#8211; 12.10.2009</h3>
<p>Updated production code with a new custom filter that removes pages that the Everyone role doesn&#8217;t have access to.<br />
Also updated the Get caching method. See <a href="http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/comment-page-1/#comment-38">comments below for further details</a>.</p>
<h3>Update &#8211; 19.09.2009</h3>
<p>Added example of production code you might consider. </p>
<p>We&#8217;ve all used Google Suggest before. When you start typing a word or sentence, Google comes up with suggestions to what we might be searching for.<br />
<img src="http://www.frederikvig.com/wp-content/uploads/suggest/google-suggest.png" alt="Example of Google Suggest" /><br />
This is to help users quickly find what they want. </p>
<p>In this blog post we&#8217;re going to do the same by extending QuickSearch that ships with the EPiServer Public Templates. </p>
<p>I decided to use <a href="http://www.jquery.com/">jQuery</a> and the <a href="http://plugins.jquery.com/project/autocompletex">AutoComplete</a> plugin for this, but you can use any script you like (most are pretty similar).</p>
<h3>The result</h3>
<p><img src="http://www.frederikvig.com/wp-content/uploads/suggest/episerver-suggest.png" alt="Example of search suggest" /></p>
<h3>The Code</h3>
<p>First start by modifying the QuickSearch.ascx:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Panel runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;QuickSearchArea&quot;</span> DefaultButton<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchButton&quot;</span><span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> AssociatedControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchText&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;hidden&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%$ Resources: EPiServer, navigation.search %&gt;&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>TextBox ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchText&quot;</span> TabIndex<span style="color: #008000;">=</span><span style="color: #666666;">&quot;1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;quickSearchField&quot;</span> autocomplete<span style="color: #008000;">=</span><span style="color: #666666;">&quot;off&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ImageButton ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ImageUrl<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Templates/Public/Images/MainMenuSearchButton.png&quot;</span> ToolTip<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%$ Resources: EPiServer, navigation.search %&gt;&quot;</span> CausesValidation<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;quickSearchButton&quot;</span> OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Search_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Panel<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008080; font-style: italic;">//&lt;![CDATA[ </span>
    $<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#&lt;%=SearchText.ClientID %&gt;&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">autocomplete</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file/SearchSuggest.aspx&quot;</span>, <span style="color: #000000;">&#123;</span>
        minChars<span style="color: #008000;">:</span> <span style="color: #FF0000;">2</span>,
        max<span style="color: #008000;">:</span> <span style="color: #FF0000;">10</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">//]]&gt; </span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span></pre></div></div>

<p>A few things to note. I removed the default text from the TextBox and added the attribute autocomplete=&#8221;off&#8221;, this is to ensure that the users browser does not store the information (<a href="https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion">How to Turn Off Form Autocompletion</a>). I then attached the AutoComplete plugin to the TextBox and changed a few of its default properties. </p>
<p>As you can see there is not much code that is needed when mostly using the default settings. You can of course do a lot more. Browse through the <a href="http://docs.jquery.com/Plugins/Autocomplete">AutoComplete plugin documentation</a> and see the <a href="http://view.jquery.com/trunk/plugins/autocomplete/demo/">demos</a> to get a few ideas.</p>
<p>Now for the main part, SearchSuggestion.aspx. This is just an empty web forms page with all the code in the code-behind:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> SearchSuggest <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// #1</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #2</span>
                var criterias <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteriaCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var queryCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                                        <span style="color: #000000;">&#123;</span>
                                            Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">StartsWith</span>,
                                            Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PageName&quot;</span>,
                                            Value <span style="color: #008000;">=</span> query,
                                            Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #FF0000;">String</span>,
                                            Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                                        <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                var pubCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                                      <span style="color: #000000;">&#123;</span>
                                          Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">Equal</span>,
                                          Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PagePendingPublish&quot;</span>,
                                          Value <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                                          Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #0000FF;">Boolean</span>,
                                          Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                                      <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>queryCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>pubCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #3</span>
                var pages <span style="color: #008000;">=</span> DataFactory.<span style="color: #0000FF;">Instance</span>.<span style="color: #0000FF;">FindPagesWithCriteria</span><span style="color: #000000;">&#40;</span>PageReference.<span style="color: #0000FF;">StartPage</span>, criterias, CurrentPage.<span style="color: #0000FF;">LanguageBranch</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> tempLimit <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> pagesCount <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #4</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>tempLimit <span style="color: #008000;">&gt;</span> pagesCount<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    tempLimit <span style="color: #008000;">=</span> pagesCount<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #5</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tempLimit<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">PageName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #6</span>
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<ul>
<li>In #1 we retrieve the user input and the limit (which we set earlier with the max property in QuickSearch.ascx), we then HTML encode it (something you should always do when receiving user input).</li>
<li>In #2 we build the criteria collection with our search options. We search for pages that start with what the user typed in and then make sure that only published pages are returned.</li>
<li>In #3 we do the actual searching with the <a href="http://sdk.episerver.com/library/cms6/html/Overload_EPiServer_DataFactory_FindPagesWithCriteria.htm">FindPagesWithCriteria method</a> where we also set the current language branch (only return pages in that language).</li>
<li>In #4 we make sure that we don&#8217;t have less pages than the limit, if so we set the pages count to be the limit.</li>
<li>In #5 we go through pages (PageDataCollection) and add the PageName to sb (StringBuilder).</li>
<li>In #6 we return the result back</li>
</ul>
<p>Below is the code for SearchSuggest.aspx.cs updated to use the SearchDataSource instead of FindPagesWithCriteria (this will not only search the PageName but all properties that are search able):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Web.WebControls</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> SearchSuggest <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var searchDataSource <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SearchDataSource
                <span style="color: #000000;">&#123;</span>
                    SearchQuery <span style="color: #008000;">=</span> query,
                    AccessLevel <span style="color: #008000;">=</span> AccessLevel.<span style="color: #0000FF;">Read</span>,
                    PublishedStatus <span style="color: #008000;">=</span> PagePublishedStatus.<span style="color: #0000FF;">Published</span>,
                    PageLink <span style="color: #008000;">=</span> PageReference.<span style="color: #0000FF;">StartPage</span>,
                    LanguageBranches <span style="color: #008000;">=</span> CurrentPage.<span style="color: #0000FF;">LanguageBranch</span>,
                    MaxCount <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PageData page <span style="color: #0600FF;">in</span> searchDataSource.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>DataSourceSelectArguments.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>page.<span style="color: #0000FF;">PageName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Production code</h3>
<p>Like I said in the beginning, this code should not be used in production scenarios since it goes through the entire site searching for matching PageNames each time there is a request. </p>
<p>I&#8217;ve added some example code of how you might approach this in a production scenario. Basically I just use the FindPagesWithCriteria method to find all the pages and then store the PageName of each in an array that I then store in the cache for easy access.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">FV.Templates.FV.Filters</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> ProductionSearchSuggestion <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> pageNames<span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> pageNamesCacheKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;AllPageNames&quot;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Get<span style="color: #000000;">&#40;</span>pageNamesCacheKey, <span style="color: #0600FF;">out</span> pageNames<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    var criterias <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteriaCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pubCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                    <span style="color: #000000;">&#123;</span>
                        Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">Equal</span>,
                        Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PagePendingPublish&quot;</span>,
                        Value <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                        Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #0000FF;">Boolean</span>,
                        Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                    criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>pubCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pages <span style="color: #008000;">=</span> DataFactory.<span style="color: #0000FF;">Instance</span>.<span style="color: #0000FF;">FindPagesWithCriteria</span><span style="color: #000000;">&#40;</span>PageReference.<span style="color: #0000FF;">StartPage</span>, criterias, CurrentPage.<span style="color: #0000FF;">LanguageBranch</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #008000;">new</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Everyone&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Filter</span><span style="color: #000000;">&#40;</span>pages<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pagesCount <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
                    pageNames <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>pagesCount<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> pagesCount<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">PageName</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
&nbsp;
                    Add<span style="color: #000000;">&#40;</span>pageNames, pageNamesCacheKey<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> tempLimit <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>tempLimit<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> resultCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> pageNames.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>resultCounter <span style="color: #008000;">&lt;=</span> tempLimit <span style="color: #008000;">&amp;&amp;</span> pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>query.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>result.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        result<span style="color: #000000;">&#91;</span>resultCounter<span style="color: #008000;">++</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> resultCounter<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>result<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Caching methods from http://johnnycoder.com/blog/2008/12/10/c-cache-helper-class/</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Exists<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> key<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Get<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> key, <span style="color: #0600FF;">out</span> T value<span style="color: #000000;">&#41;</span> where T<span style="color: #008000;">:</span><span style="color: #FF0000;">class</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                value <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> T<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    value <span style="color: #008000;">=</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span>
            <span style="color: #000000;">&#123;</span>
                value <span style="color: #008000;">=</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Add<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T o, <span style="color: #FF0000;">string</span> key<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span>
                key,
                o,
                <span style="color: #0600FF;">null</span>,
                DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">AddMinutes</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3600</span><span style="color: #000000;">&#41;</span>,
                <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">NoSlidingExpiration</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The FilterRole class</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Filters</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FilterRole <span style="color: #008000;">:</span> IPageFilter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> RoleName
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span>
            set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> roleName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RoleName</span> <span style="color: #008000;">=</span> roleName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Filter<span style="color: #000000;">&#40;</span>PageDataCollection pages<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">bool</span> isMatch <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
                var acl <span style="color: #008000;">=</span> pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ACL</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>acl <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var role <span style="color: #0600FF;">in</span> acl<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>role.<span style="color: #0000FF;">Key</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RoleName</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            isMatch <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                        <span style="color: #000000;">&#125;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>isMatch<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    pages.<span style="color: #0000FF;">RemoveAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Filter<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, FilterEventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Filter</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Pages</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> ShouldFilter<span style="color: #000000;">&#40;</span>PageData page<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href="http://www.frederikvig.com/wp-content/uploads/suggest/SearchSuggest.zip">Download the code</a><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/06/using-multiple-forms-on-an-asp-net-web-forms-page/" rel="bookmark" title="June 21, 2009">Using multiple forms on an ASP.NET web forms page</a></li>
<li><a href="http://www.frederikvig.com/2009/10/episerver-findpageswithcriteria-and-findallpageswithcriteria/" rel="bookmark" title="October 21, 2009">EPiServer FindPagesWithCriteria and FindAllPagesWithCriteria</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/09/creating-a-custom-episerver-paging-control/" rel="bookmark" title="September 20, 2009">Creating a Custom EPiServer Paging Control</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-5-creating-the-search-page-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 16, 2009">Part 5: Creating the search page – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 22.879 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->