<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>A .NET Coders Blog</title>
	<atom:link href="http://jbardrof.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jbardrof.wordpress.com</link>
	<description>Life with .NET</description>
	<lastBuildDate>Wed, 04 Jun 2008 14:16:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jbardrof.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>A .NET Coders Blog</title>
		<link>http://jbardrof.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jbardrof.wordpress.com/osd.xml" title="A .NET Coders Blog" />
	<atom:link rel='hub' href='http://jbardrof.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Realizing DataAware UserControls in ASP.NET MVC</title>
		<link>http://jbardrof.wordpress.com/2008/06/03/realizing-dataaware-usercontrols-in-aspnet-mvc/</link>
		<comments>http://jbardrof.wordpress.com/2008/06/03/realizing-dataaware-usercontrols-in-aspnet-mvc/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 12:45:19 +0000</pubDate>
		<dc:creator>jbardrof</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jbardrof.wordpress.com/?p=4</guid>
		<description><![CDATA[For my own part I&#8217;ve always been interested in creating web applications which could move UserControls all over the place more in line with the way a client application can move controls around. The idea of using a page as more of a container which allows for UserControls to be shuttled in and out has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jbardrof.wordpress.com&amp;blog=3602201&amp;post=4&amp;subd=jbardrof&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For my own part I&#8217;ve always been interested in creating web applications which could move UserControls all over the place more in line with the way a client application can move controls around.  The idea of using a page as more of a container which allows for UserControls to be shuttled in and out has always been very appealing to me.  Webforms came close with the idea of WebParts, but my mileage never seemed to add up for them.  With the new ASP.NET MVC pattern I&#8217;ve been very interested in the new possibility of rendering UserControls on the fly.  Unfortunately the default behavior only allows us to render usercontrols from the ViewPage and at the most, send them ViewData which must come from the ViewPage.  That may not be ideal as we may want to have UserControls on a page that are self data aware whom have the ability to act independently of the ViewPage, such as the infamous stock ticker example.  The ComponentController was added to add that sort of functionality to the MVC framework, but for my own purposes I&#8217;ve found that it falls short.  The first problem is that actions on a ComponentController can not be called the same way as those from a regular Controller, in fact, the ComponentController does not even implement the IController interface.  That may not seem like much, but as soon as I attempted to use some ajax. the ComponentController fell apart.  Consider this:<br />
<code><br />
<span style="color:#ffff00;">&lt;%</span><span style="color:#ffffff;">=<span style="color:#ffff99;">Html.RenderComponent&lt;</span></span><span style="color:#3366ff;">StockComponentController</span><span style="color:#ffff99;">&gt;(c =&gt; c.ShowRecentStocks())</span><span style="color:#ffff00;">%&gt;</span></code></p>
<p>We&#8217;ve got ourselves a page which acts as a component and shows some stocks.  Now lets put a link in that component view to show a specific stock, and lets say we want to call that using ajax (I&#8217;m using jQuery for my examples)</p>
<p><code><span style="color:#ffff99;">$(</span><span style="color:#ff9900;">"#stockDiv"</span><span style="color:#ffff99;">).load(</span><span style="color:#ff9900;">'/StockComponentController/DisplaySymbol'</span><span style="color:#ffff99;">);</span></code></p>
<p>We won&#8217;t get very far here because the ComponentController can&#8217;t accept a call to it in this fashion, therefore calling back to our component in any fashion other than a Html.RenderComponent call won&#8217;t work.  I also don&#8217;t like this behavior because we have to use aspx pages with ComponentControllers, it just feels more appropriate to use an ascx.  Also if we use a regular Controller we can do what was posted above much more easily.  We can through ajax refresh the UserControl by using the default action behavior.  This works quite well. for example:</p>
<p><code><span style="color:#ffff99;">$(document).ready(</span><span style="color:#00ccff;">function</span><span style="color:#ffff99;">(){</span><br />
<span style="color:#00ccff;"> var</span> <span style="color:#ffff99;">symbol=</span> <span style="color:#ff9900;">"sym="</span> <span style="color:#ffff99;">+ $(</span><span style="color:#ff9900;">"#txtSymbol"</span><span style="color:#ffff99;">).</span><span style="color:#00ccff;">get</span><span style="color:#ffff99;">(0).value;</span><br />
<span style="color:#ffff99;">$(</span><span style="color:#ff9900;">"#stockDiv"</span><span style="color:#ffff99;">).load(</span><span style="color:#ff9900;">'/User/GetSymbol/'</span><span style="color:#ffff99;"> + symbol);</span> <span style="color:#ffff99;"> });</span></code></p>
<p>UserControls actually work rather well except for the initial page load. We&#8217;re forced to pass them ViewData from the ViewPage, whereas ComponentControllers work well for the page load but fall apart if we want to interact with them.  With that in mind I&#8217;ve &#8220;frankensteined&#8221; together the missing piece.  What could work really well is a RenderControl helper which acts more like the RenderComponent, but allows us to render anything which derives from a controller.  With this we can write an action which renders to a ViewUserControl and place that nicely right where we like it:</p>
<p><code> </code></p>
<p><span style="color:#3366ff;">public static string </span><span style="color:#ffff99;">RenderControl&lt;TController&gt;(</span><span style="color:#3366ff;">this</span> <span style="color:#99ccff;">HtmlHelper</span> <span style="color:#ffff99;">helper, </span><span style="color:#99ccff;">Expression</span><span style="color:#ffff99;">&lt;</span><span style="color:#3366ff;">Action</span><span style="color:#ffff99;">&lt;TController&gt;&gt;</span> <span style="color:#ffff99;"><span style="color:#ffff99;">action)</span> </span><span style="color:#3366ff;">where</span> <span style="color:#ffff99;">TController : </span><span style="color:#99ccff;">Controller</span><span style="color:#ffff99;">, </span><span style="color:#3366ff;">new</span><span style="color:#ffff99;">()<br />
{</span></p>
<p style="padding-left:30px;"><span style="color:#ffff99;"> TController local = <span style="color:#99ccff;">Activator</span>.CreateInstance&lt;TController&gt;();</span><br />
<span style="color:#ffff99;"> local.ControllerContext = helper.ViewContext;</span><br />
<span style="color:#ffff99;">MethodCallExpression</span> <span style="color:#ffff99;">body = action.Body</span> <span style="color:#3366ff;">as</span> <span style="color:#99ccff;">MethodCallExpression</span><span style="color:#ffff99;">;</span><br />
<span style="color:#3366ff;">string </span><span style="color:#ffff99;">name = body.Method.Name;</span><br />
<span style="color:#99ccff;">MethodInfo </span><span style="color:#ffff99;">method = local.GetType().GetMethod(name);</span><br />
<span style="color:#99ccff;">List</span><span style="color:#ffff99;">&lt;</span><span style="color:#3366ff;">object</span><span style="color:#ffff99;">&gt;</span> <span style="color:#ffff99;">list =</span> <span style="color:#3366ff;">new</span> <span style="color:#99ccff;">List</span><span style="color:#ffff99;">&lt;</span><span style="color:#3366ff;">object</span><span style="color:#ffff99;">&gt;();</span><br />
<span style="color:#ccffff;"> foreach</span> <span style="color:#ffff99;">(</span><span style="color:#99ccff;">Expression</span> <span style="color:#ffff99;">exp </span><span style="color:#99ccff;">in </span><span style="color:#ffff99;">body.Arguments)<br />
{</span></p>
<p style="padding-left:60px;"><span style="color:#99ccff;"> ConstantExpression</span> <span style="color:#ffff99;">exp1 = (</span><span style="color:#99ccff;">ConstantExpression</span><span style="color:#ffff99;">)exp;</span><br />
<span style="color:#3366ff;">object</span> <span style="color:#ffff99;">item</span> <span style="color:#ffff99;">= exp1.Value;<br />
list.Add(item);</span></p>
<p style="padding-left:30px;"><span style="color:#ffff99;">}</span><br />
<span style="color:#ccffff;">ActionResult</span> <span style="color:#ffff99;">result = (</span><span style="color:#99ccff;">ActionResult</span><span style="color:#ffff99;">)method.Invoke(local, list.ToArray());<br />
result.ExecuteResult(local.ControllerContext);</span><br />
<span style="color:#3366ff;">return string</span><span style="color:#ffff99;">.Empty;<br />
}</span></p>
<p>There are a few caveats that I&#8217;m not particularly thrilled with and haven&#8217;t had the chance to refine as of yet.  I took the RenderComponent helper and translated it to work for a Controller. As you can tell the return is the sticky part.  The html helper expects a string return, but the ExecuteResult method uses the ViewEngine to output its html.  With that behavior we&#8217;ve already output out html by the result.Execute&#8230;. line, but if we set the return value to void, it throws off the HtmlHelper behavior.  I&#8217;m pretty sure that can be ammended.</p>
<p>Now we can write:</p>
<p><code><span style="color:#ffff00;">&lt;%</span><span style="color:#ffff99;">=Html.RenderControl&lt;</span><span style="color:#99ccff;">StockController</span><span style="color:#ffff99;">&gt;(c =&gt; c.ShowRecentStocks())</span><span style="color:#ffff00;"> %&gt;</span></code></p>
<p>This gives us all the benefits of using ViewUserControls and Controllers which I find superior to ComponentControllers and ViewPages.  It feels counterintuitive to put ViewPages inside ViewPages and avoiding it while still getting the ability to have actions which partially render pages is excellent.  I have not tested this for every situation but I do believe it should work as needed.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jbardrof.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jbardrof.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbardrof.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbardrof.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbardrof.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jbardrof.wordpress.com&amp;blog=3602201&amp;post=4&amp;subd=jbardrof&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jbardrof.wordpress.com/2008/06/03/realizing-dataaware-usercontrols-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9797a726a1bcac449f6f12f335b47331?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbardrof</media:title>
		</media:content>
	</item>
		<item>
		<title>Why Blog?</title>
		<link>http://jbardrof.wordpress.com/2008/04/29/hello-world/</link>
		<comments>http://jbardrof.wordpress.com/2008/04/29/hello-world/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 00:51:17 +0000</pubDate>
		<dc:creator>jbardrof</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[This is a question I&#8217;ve asked myself a few times, and its time to get started. With my recent involvement regarding the ASP.NET MVC framework and Silverlight, I&#8217;ve gotten quite excited to do something new. In the past I&#8217;ve felt as I&#8217;m sure many do that there are more writers than readers and writing a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jbardrof.wordpress.com&amp;blog=3602201&amp;post=1&amp;subd=jbardrof&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a question I&#8217;ve asked myself a few times, and its time to get started.  With my recent involvement regarding the ASP.NET MVC framework and Silverlight, I&#8217;ve gotten quite excited to do something new.  In the past I&#8217;ve felt as I&#8217;m sure many do that there are more writers than readers and writing a blog is a tad bit narcissistic.  Although, if we can give our writing direction, and serve a purpose maybe then our writing can be useful.  My goal is to write about experiences I have with .NET that maybe haven&#8217;t been written about everywhere, and hopefully provide some help back to the community.  I hope my forthcoming posts will accomplish that goal. Stick around!</p>
<p>Jeremy</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jbardrof.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jbardrof.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbardrof.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbardrof.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbardrof.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jbardrof.wordpress.com&amp;blog=3602201&amp;post=1&amp;subd=jbardrof&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jbardrof.wordpress.com/2008/04/29/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9797a726a1bcac449f6f12f335b47331?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbardrof</media:title>
		</media:content>
	</item>
	</channel>
</rss>
