<?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>SquaredRoot &#187; codebehind</title>
	<atom:link href="http://www.squaredroot.com/tag/codebehind/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.squaredroot.com</link>
	<description>.Net Development in DC</description>
	<lastBuildDate>Sun, 16 Aug 2009 01:30:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MVC: Strongly Typed ViewData Without A Code-Behind</title>
		<link>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/</link>
		<comments>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 03:00:25 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[codebehind]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">/post/2008/01/07/MVC-Strongly-Type-ViewPages-Without-A-Code-Behind.aspx</guid>
		<description><![CDATA[If you&#8217;ve tried to use strongly-typed ViewData with a ViewPage or ViewContentPage without using a code-behind file you may have run into the curious scenario of how to specify a generic in the &#60;%@ Page %&#62; element&#8217;s Inherits attribute. The Problem Let&#8217;s say you wanted to specify that the ViewData for this view would be [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve tried to use strongly-typed <strong>ViewData</strong> with a <strong>ViewPage</strong> or <strong>ViewContentPage</strong> without using a code-behind file you may have run into the curious scenario of how to specify a generic in the <strong>&lt;%@ Page %&gt;</strong> element&#8217;s <strong>Inherits</strong> attribute.</p>
<h2>The Problem</h2>
<p>Let&#8217;s say you wanted to specify that the <strong>ViewData</strong> for this view would be an integer. Normally you would specify this in the code-behind like so:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> <span class="kwrd">using</span> System;</pre>
<pre class="alteven"><span class="lnum">   2:</span> <span class="kwrd">using</span> System.Web;</pre>
<pre class="alt"><span class="lnum">   3:</span> <span class="kwrd">using</span> System.Web.Mvc;</pre>
<pre class="alteven"><span class="lnum">   4:</span></pre>
<pre class="alt"><span class="lnum">   5:</span> <span class="kwrd">namespace</span> MVC_Example.Views.Home</pre>
<pre class="alteven"><span class="lnum">   6:</span> {</pre>
<pre class="alt"><span class="lnum">   7:</span>     <span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Index : ViewPage&lt;<span class="kwrd">int</span>&gt;</pre>
<pre class="alteven"><span class="lnum">   8:</span>     {</pre>
<pre class="alt"><span class="lnum">   9:</span>     }</pre>
<pre class="alteven"><span class="lnum">  10:</span> }</pre>
</div>
</div>
<p>Since we don&#8217;t have a code-behind file though, we have to change the .<em>aspx</em> file&#8217;s <strong>&lt;%@ Page %&gt;</strong> element. Trying to set it to the following will not work:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> &lt;%@ Page Language=<span class="str">"C#"</span> Inherits=<span class="str">"MVC_Example.Views.Home.Index&lt;int&gt;"</span> %&gt;</pre>
</div>
</div>
<h2>The Basic Answer</h2>
<p>The value of the <strong>Inherits</strong> attribute appears to be delivered straight to the CLR without any language specific parsing, which means we have to specify it the way the CLR wants to interpret it. I browsed around and found (<a href="http://forums.asp.net/t/1193721.aspx">here</a> and <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071&amp;wa=wsignin1.0">here</a>) some information that points out the proper way to do this:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> &lt;%@ Page Language=<span class="str">"C#"</span> Inherits=<span class="str">"System.Web.Mvc.ViewPage`1[ [System.Int32,mscorlib] ]"</span> %&gt;</pre>
</div>
</div>
<h2>WTF?</h2>
<p>Eww, huh? The syntax is gnarly, but it gets the job done. Let&#8217;s break the syntax down to make sure you understand what is going on:</p>
<p><strong><em>ParentType`1[ [ChildType,ChildTypeAssembly] ]</em></strong></p>
<ul>
<li><em>&#8220;ParentType&#8221;</em> is fairly obvious. This is the generic object that you want to create a reference to.</li>
<li><em>&#8220;`1[ ["</em> is a bit more obscure. The "`1" specifies the number of generics in this argument, which we'll come back to in a second. The double square brackets are required. A single square bracket will <strong>NOT</strong> work.</li>
<li><em>"ChildType"</em> is the type of object you wish to have the generic consume (what is the proper terminology here? anyone know?).</li>
<li><em>"ChildTypeAssembly"</em> is the name of the assembly (<strong>NOT</strong> the namespace) that contains the <em>ChildType</em>. All of your common value types will be located in "<strong>mscorlib</strong>".</li>
</ul>
<h2>Multiple Generic Types</h2>
<p>Let's say <strong>ViewPage</strong> took two arguments as part of its generic declaration area (ie: <strong>ViewPage&lt;X,Y&gt;</strong>). It doesn't, by the way, but we'll pretend it does for the sake of argument; you may find this information useful someday.</p>
<p>To have two type parameters, you'll have to change the <strong>"`1"</strong> to a <strong>"`2"</strong> and add an extra <strong>"[ChildType, ChildTypeAssembly]&#8220;</strong> reference, separated by a space. More parameters would continue to follow the same logic.</p>
<p>If you wanted to achieve the equivalent of <strong>ViewPage&lt;int,string&gt;</strong> it would look like this:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> System.Web.Mvc.ViewPage`2[ [System.Int32,mscorlib], [System.String,mscorlib] ]</pre>
</div>
</div>
<h2>A Generic of Generics</h2>
<p>A more likely scenario is that you may want to have your <strong>ViewData</strong> be a generic <strong>List&lt;T&gt;</strong>. In this case you&#8217;ll just nest the declarations. If you wanted to pass <strong>ViewData</strong> of the type <strong>List&lt;string&gt;</strong> to the <strong>ViewPage</strong> it would look like:</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:</span> System.Web.Mvc.ViewPage`1[[System.Collections.Generic.List`1[[System.String,mscorlib]], mscorlib]]</pre>
</div>
</div>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/01/07/mvc-strongly-type-viewpages-without-a-code-behind/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>MVC Template Fix</title>
		<link>http://www.squaredroot.com/2008/01/02/mvc-template-fix/</link>
		<comments>http://www.squaredroot.com/2008/01/02/mvc-template-fix/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 02:51:00 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[codebehind]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">/post/2008/01/02/MVC-Template-Fix.aspx</guid>
		<description><![CDATA[Several weeks ago I blogged about a bug I found in the CTP release of the MVC framework. The gist of the bug is that controls declared in the html portion of a page or user control could not be referenced from the code-behind of that page or user control. ScottGu posted a comment clarifying [...]]]></description>
			<content:encoded><![CDATA[<p>
Several weeks ago <a href="/post/2007/12/MVC-Bug-Broken-Codebehind.aspx">I blogged about a bug</a> I found in the <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">CTP release of the MVC framework</a>. The gist of the bug is that controls declared in the html portion of a page or user control could not be referenced from the code-behind of that page or user control. ScottGu posted a comment clarifying that <a href="/post/2007/12/MVC-Bug-Broken-Codebehind.aspx#Comment2e9c6968-9dc1-4d6d-b75b-60dc6335af1f">this was due to a bug in the templates</a> released in the CTP.
</p>
<p>
After nearly a month of following Scott&#39;s advice on how to fix the bug, I finally grew tired of such a repetitive task and have fixed the templates. You&#39;ll find a zip file containing the fix and installation instructions at the bottom of this post. Before I get into that though, let me explain the workaround (suggested by ScottGu) that I&#39;ve been using until now:
</p>
<p>
<strong>The Easy, Repetitive Workaround</strong>
</p>
<p>
The problem is that the template for pages, master pages, and user controls only include the declarative HTML file and it&#39;s code-behind. The template is missing the designer file used by Visual Studio to hide the references generated by the server controls declared in your HTML. Here is what a MVC page looks like right after creation:
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-Before_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-Before_thumb.jpg" border="0" alt="SimpleWorkaround-Before" width="179" height="65" /></a>
</p>
<p>
You could manually create a designer class to fix this issue, but that would be a pain. Luckily Visual Studio provides us with the handy &quot;Convert to Web Application&quot; feature to generate the designer class for us. Simply right-click on the .ASPX (or .Master or .ASCX) and click the &quot;Convert to Web Application&quot; option on the context menu.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-During_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-During_thumb.jpg" border="0" alt="SimpleWorkaround-During" width="224" height="153" /></a>
</p>
<p>
After doing so, you should see this:
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-After_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/SimpleWorkaround-After_thumb.jpg" border="0" alt="SimpleWorkaround-After" width="224" height="80" /></a>
</p>
<p>
<em>Voil&agrave;</em>! Your codebehind file for that page/control (and that page/control only) should now work as expected. Its quick and simple, but I&#39;m sure you can imagine already the annoyance at having to do this for each and every page or control you create. That thought leads us to&#8230;
</p>
<p>
<strong>The Slightly More Complex Permanent Solution</strong>
</p>
<p>
First, an admission of guilt: I only fixed the C# templates. I apologize to any VB.Net&#39;ers out there ahead of time.
</p>
<p>
There are four templates that must be fixed:
</p>
<ol>
<li>View Master Page &#8211; A master page.</li>
<li>View Page &#8211; A normal page.</li>
<li>View Content Page &#8211; A page that uses a master page.</li>
<li>View User Control &#8211; A control.</li>
</ol>
<p>
These four templates are found in two different places: the <em>item templates</em> folder and the item <em>templates cache</em> folder.
</p>
<p>
Those folders are found at: &quot;<em>Common7IDEItemTemplatesCSharpWeb1033&quot; </em>and &quot;<em>Common7IDEItemTemplatesCacheCSharpWeb1033&quot;</em> respectively (within your VS 9 root folder).
</p>
<p>
The item templates folder contains zip files which are at some point unzipped and stored in the item <em>templates cache</em> folder. I suppose you could just update the contents of the <em>item templates cache</em> folder, but I don&#39;t know if those settings would ever be written over by the <em>item templates</em> folder, so it is better to be safe and fix it in both places.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-ZipLocation_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-ZipLocation_thumb.jpg" border="0" alt="TemplateFix-ZipLocation" width="244" height="146" /></a>
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-CacheLocation_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-CacheLocation_thumb.jpg" border="0" alt="TemplateFix-CacheLocation" width="244" height="168" /></a>
</p>
<p>
The contents of a typical template for pages/user controls are:
</p>
<ul>
<li>a <em>vtemplate</em> file (which serves as the manifest for the rest of the items)</li>
<li>an icon file</li>
<li>an html file (ASPX, ASCX, etc)</li>
<li>a codebehind file</li>
<li>a designer file</li>
</ul>
<p>
As you can see from the screenshot below, the templates did not include a designer file.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-ZipContents_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-ZipContents_thumb.jpg" border="0" alt="TemplateFix-ZipContents" width="244" height="107" /></a>
</p>
<p>
In addition, the vtemplate file does not describe a designer file.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-TemplateFile_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-TemplateFile_thumb.jpg" border="0" alt="TemplateFix-TemplateFile" width="435" height="49" /></a>
</p>
<p>
The fix is to add a line to the vtemplate file referencing a designer file and then add the designer file itself to the template folder/zip. The designer file should look something like:
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-DesignerFile_2.jpg"><img style="border: 0px none " src="/image.axd?picture=WindowsLiveWriter/MVCTemplateFix_137BB/TemplateFix-DesignerFile_thumb.jpg" border="0" alt="TemplateFix-DesignerFile" width="244" height="69" /></a>
</p>
<p>
I&#39;ve gone ahead and done all this for you &#8212; well, actually for me, but you get the point. <img src='http://www.squaredroot.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  All you need to do is download the zip file from below and follow the instructions included in the readme.txt file.
</p>
<p>
<a rel="enclosure" href="/file.axd?file=MVC+Template+Fix.zip">MVC Template Fix.zip (50.54 kb)</a></p>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/01/02/mvc-template-fix/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/01/02/mvc-template-fix/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/01/02/mvc-template-fix/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/01/02/mvc-template-fix/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/01/02/mvc-template-fix/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MVC Bug: Broken Codebehind</title>
		<link>http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/</link>
		<comments>http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 17:56:00 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[codebehind]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">/post/2007/12/11/MVC-Bug-Broken-Codebehind.aspx</guid>
		<description><![CDATA[Update (Jan&#160;2): I have posted a permanent fix in the form&#160;of modified&#160;Visual Studio templates in another blog post.&#160; Update (Dec 11): ScottGu was kind enough to post a fix for this particular problem. It is unfortunate that you have to repeat the fix for every view you add to your project, but I guess that&#39;s [...]]]></description>
			<content:encoded><![CDATA[<p>
<strong>Update (Jan&nbsp;2):</strong> I have posted <a href="/post/2008/01/MVC-Template-Fix.aspx">a permanent fix in the form&nbsp;of modified&nbsp;Visual Studio templates in another blog post</a>.<strong>&nbsp;</strong>
</p>
<p>
<strong>Update (Dec 11):</strong> ScottGu was kind enough to <a href="/post/2007/12/MVC-Bug-Broken-Codebehind.aspx#Comment2e9c6968-9dc1-4d6d-b75b-60dc6335af1f">post a fix for this particular problem</a>. It is unfortunate that you have to repeat the fix for every view you add to your project, but I guess that&#39;s why it is called a CTP! ScottGu said&#8230;
</p>
<blockquote>
<p>
	There is a bug in the file template when you create a new page &#8211; and the .designer.cs file isn&#39;t generated. <br />
	To fix this, right click on the file and choose the &quot;Convert to Web Application&quot; menu item. This will generate the .designer.cs file that contains your control declarations. From that point on the code-behind will be kept up to date as you make changes.
	</p>
<p>
	<br />
	Hope this helps,
	</p>
<p>
	<br />
	Scott
	</p>
</blockquote>
<hr size="1" />
</p>
<p>
<strong>Original Post:</strong>&nbsp;
</p>
<p>
It may not be a popular choice, but I&#39;m perfectly okay with in-line code in my views as long as it doesn&#39;t contain business logic. One of the developers on my project prefers tag-based views (&lt;asp:Blah runat=&quot;server&quot; /&gt;) and came to me yesterday with a curious issue. I&#39;ve spent some of the morning investigating the issue and it does appear that there is a bug in the <a href="http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx">newly released ASP.Net MVC Framework</a>.
</p>
<p>
The issue? <strong>Controls declared in an ASPX are not visible to that page&#39;s codebehind.</strong>
</p>
<p>
To test this hypothesis (and make sure we hadn&#39;t somehow broken our project) I started a new &quot;ASP.Net MVC Web Application&quot; project (note that the bug also exists for the &quot;ASP.Net MVC Web Application and Test&quot; project). I then opened the Views/Home/About.aspx file and added the following line:
</p>
<p><!--<br />
{rtf1ansiansicpglang1024noproof65001uc1 deff0{fonttbl{f0fnilfcharset0fprq1 Courier New;}}{colortbl;??red0green0blue0;red255green238blue98;red0green0blue255;red255green255blue255;red163green21blue21;red255green0blue0;}??fs20 cb2highlight2 &lt;%cf3cb0highlight0 @cf0  cf5 Pagecf0  cf6 Languagecf3 ="C#"cf0  cf6 MasterPageFilecf3 ="~/Views/Shared/Site.Master"cf0  cf6 AutoEventWireupcf3 ="true"cf0  cf6 CodeBehindcf3 ="About.aspx.cs"cf0  cf6 Inheritscf3 ="MVCBug.Views.Home.About"cf0  cb2highlight2 %&gt;par ??par ??cf3cb0highlight0 &lt;cf5 aspcf3 :cf5 Contentcf0  cf6 IDcf3 ="Content2"cf0  cf6 ContentPlaceHolderIDcf3 ="MainContentPlaceHolder"cf0  cf6 runatcf3 ="server"&gt;par ??par ??cf0     cf3 &lt;cf5 h2cf3 &gt;cf0 Todo: Company informationcf3 &lt;/cf5 h2cf3 &gt;par ??cf0 tab cf3 &lt;cf5 aspcf3 :cf5 Literalcf0  cf6 IDcf3 ="myLiteral"cf0  cf6 runatcf3 ="server"cf0  cf6 Textcf3 ="Hello World"cf0  cf3 /&gt;par ??cf0     par ??cf3 &lt;/cf5 aspcf3 :cf5 Contentcf3 &gt;}<br />
--></p>
<div style="background: white none repeat scroll 0% 0%; font-size: 9pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new">
<p style="margin: 0px">
<span style="background: #ffee62 none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">&lt;%</span><span style="color: blue">@</span> <span style="color: #a31515">Page</span> <span style="color: red">Language</span><span style="color: blue">=&quot;C#&quot;</span> <span style="color: red">MasterPageFile</span><span style="color: blue">=&quot;~/Views/Shared/Site.Master&quot;</span> <span style="color: red">AutoEventWireup</span><span style="color: blue">=&quot;true&quot;</span> <span style="color: red">CodeBehind</span><span style="color: blue">=&quot;About.aspx.cs&quot;</span> <span style="color: red">Inherits</span><span style="color: blue">=&quot;MVCBug.Views.Home.About&quot;</span> <span style="background: #ffee62 none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">%&gt;</span>
</p>
<p style="margin: 0px">
<span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Content</span> <span style="color: red">ID</span><span style="color: blue">=&quot;Content2&quot;</span> <span style="color: red">ContentPlaceHolderID</span><span style="color: blue">=&quot;MainContentPlaceHolder&quot;</span> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;&gt;</span>
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>Todo: Company information<span style="color: blue">&lt;/</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Literal</span> <span style="color: red">ID</span><span style="color: blue">=&quot;myLiteral&quot;</span> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;</span> <span style="color: red">Text</span><span style="color: blue">=&quot;Hello World&quot;</span> <span style="color: blue">/&gt;</span>
</p>
<p style="margin: 0px">
<span style="color: blue">&lt;/</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Content</span><span style="color: blue">&gt;</span>
</p>
</div>
<p>
&nbsp;
</p>
<p>
Then I opened up the page&#39;s codebehind (Views/Home/About.aspx.cs) and added the following:
</p>
<p><!--<br />
{rtf1ansiansicpglang1024noproof65001uc1 deff0{fonttbl{f0fnilfcharset0fprq1 Courier New;}}{colortbl;??red0green0blue255;red255green255blue255;red0green0blue0;red43green145blue175;red163green21blue21;}??fs20 cf1 usingcf0  System;par ??cf1 usingcf0  System.Web;par ??cf1 usingcf0  System.Web.Mvc;par ??par ??cf1 namespacecf0  MVCBug.Views.Homepar ??{par ??    cf1 publiccf0  cf1 partialcf0  cf1 classcf0  cf4 Aboutcf0  : cf4 ViewPagepar ??cf0     {par ??        cf1 publiccf0  cf1 voidcf0  Page_Load()par ??        {par ??tab tab tab myLiteral.Text = cf5 "Goodbye World"cf0 ;par ??        }par ??    }par ??}}<br />
--></p>
<div style="background: white none repeat scroll 0% 0%; font-size: 9pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new">
<p style="margin: 0px">
<span style="color: blue">using</span> System;
</p>
<p style="margin: 0px">
<span style="color: blue">using</span> System.Web;
</p>
<p style="margin: 0px">
<span style="color: blue">using</span> System.Web.Mvc;
</p>
<p style="margin: 0px">
<span style="color: blue">namespace</span> MVCBug.Views.Home
</p>
<p style="margin: 0px">
{
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">partial</span> <span style="color: blue">class</span> <span style="color: #2b91af">About</span> : <span style="color: #2b91af">ViewPage</span>
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp; {
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> Page_Load()
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myLiteral.Text = <span style="color: #a31515">&quot;Goodbye World&quot;</span>;
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
</p>
<p style="margin: 0px">
&nbsp;&nbsp;&nbsp; }
</p>
<p style="margin: 0px">
}
</p>
</div>
<p>
&nbsp;
</p>
<p>
Ctrl+Shift+B to compile and bam, a build error: <strong>&quot;The name &#39;myLiteral&#39; does not exist in the current context.&quot;</strong>
</p>
<p>
I began to wonder if this was not supported by the MVC framework, but I took a look back at one of ScottGu&#39;s most recent articles, <a href="http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views.aspx">ASP.Net MVC Framework (Part 3): Passing ViewData from Controllers to Views</a>, and saw that he references controls from the page&#39;s codebehind several times. I also tried using several controls besides the Literal to no avail.
</p>
<p>
To test what was going on, I commented out the reference in the Page_Load method, added a string declaration and put a breakpoint on it. When I ran the MVC application in debug mode and loaded the page I was able to see the &quot;myLiteral&quot; control reference. It appears that the reference is available at runtime but Visual Studio just is not able to see it at compile time. Odd.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/MVCBugBrokenCodebehind_C282/DebugMyLiteral.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/MVCBugBrokenCodebehind_C282/DebugMyLiteral_thumb.jpg" border="0" alt="DebugMyLiteral" width="446" height="266" /></a>
</p>
<p>
For now I&#39;ve told the developer to just use in-line code, but I&#39;m well aware that many developers are loathe to do so. Thoughts? Suggestions?</p>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2007/12/11/mvc-bug-broken-codebehind/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
