<?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>Jon Kragh's Blog &#187; .Net</title>
	<atom:link href="http://www.jonkragh.com/index.php/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jonkragh.com</link>
	<description>I think I can, I know I can</description>
	<lastBuildDate>Mon, 02 Nov 2009 11:55:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rendering an ASP.net UserControl to a String</title>
		<link>http://www.jonkragh.com/index.php/rendering-an-asp-net-usercontrol-to-a-string/</link>
		<comments>http://www.jonkragh.com/index.php/rendering-an-asp-net-usercontrol-to-a-string/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 11:56:02 +0000</pubDate>
		<dc:creator>Jon Kragh</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.jonkragh.com/index.php/rendering-an-asp-net-usercontrol-to-a-string/</guid>
		<description><![CDATA[I have been converting one of my side projects from ASP.Net WebForms to ASP.Net MVC.&#160; In order to reuse some of my existing ASP.net UserControls from WebForms in ASP.Net MVC, I tweaked a rending method found here and here.
I improved on these methods by enabling the caller of the utility function to be the one [...]]]></description>
			<content:encoded><![CDATA[<p>I have been converting one of my side projects from ASP.Net WebForms to ASP.Net MVC.&#160; In order to reuse some of my existing ASP.net UserControls from WebForms in ASP.Net MVC, I tweaked a rending method found <a href="http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx">here</a> and <a href="http://stackoverflow.com/questions/258877/load-a-user-control-programmatically-in-to-a-html-text-writer">here</a>.</p>
<p>I improved on these methods by enabling the caller of the utility function to be the one to set properties on the UserControl in a strongly typed fashion. The is no reflection or any other special interface needed on the user control.&#160; The key was adding a callback.</p>
<p>Here is what it looks like to call and render the control to a string by the caller.&#160; In this example I am rendering my GoogleMap UserControl to a string.&#160; The last argument I am passing is an anonymous method that is called by the utility function to initialize my control.</p>
<pre class="brush: csharp; gutter: false;">UIUtil.RenderUserControl&lt;GoogleMap&gt;(&quot;~/UserControls/GoogleMap.ascx&quot;,
    uc =&gt;
    {
        uc.CollegeToShow = CollegeToShow;
        uc.Height = Height;
        uc.Width = Width;
        uc.Mode = Mode;
    });</pre>
<p>The helper method is as follows:</p>
<pre class="brush: csharp; gutter: false;">public delegate void InitializeControlDelegate&lt;T&gt;(T ControlToUse);

public static string RenderUserControl&lt;T&gt;(string ControlPath, InitializeControlDelegate&lt;T&gt; InitControlCallback) where T : UserControl
{
    System.Web.UI.Page pageHolder = new Page();
    T ControlToRender = (T)pageHolder.LoadControl(ControlPath);
    pageHolder.Controls.Add(ControlToRender);
    InitControlCallback.Invoke(ControlToRender);
    StringWriter result = new StringWriter();
    System.Web.HttpContext.Current.Server.Execute(pageHolder, result, false);
    return result.ToString();
}</pre>
<p>This method uses a similar technique as to the methods linked above, but it has a callback which calls back the anonymous method that I declared in the caller.&#160; The callback method passes back a strongly typed UserControl as the argument that we can use to set properties on the initialized UserControl.</p>
<p>That is all you need to make it all work.&#160; To make things a little nicer for myself I added “RenderToString” methods on my UserControls.&#160; If you can’t update the UserControls themselves you can just stick code like this in a helper library.</p>
<pre class="brush: csharp; gutter: false;">public partial class GoogleMap : System.Web.UI.UserControl
{
    public static string RenderToString(Item ItemToShow, string Height, string Width, Constants.MapMode Mode)
    {
        return UIUtil.RenderUserControl&lt;GoogleMap&gt;(&quot;~/UserControls/GoogleMap.ascx&quot;,
            uc =&gt;
            {
                uc.ItemToShow = ItemToShow;
                uc.Height = Height;
                uc.Width = Width;
                uc.Mode = Mode;
            });
    }
}</pre>
<p>Now from within my Asp.Net MVC page (or any place else), I can render my ASP.net WebForms UserControl Google Map like so:</p>
<pre class="brush: csharp; gutter: false;">&lt;%=GoogleMap.RenderToString(ItemToUse, &quot;250px&quot;, &quot;100%&quot;, Constants.MapMode.SingleItem)%&gt; </pre>
<p>Cheers,</p>
<p>Jon</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonkragh.com/index.php/rendering-an-asp-net-usercontrol-to-a-string/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>iPhone Development, a bad use of time for a .NET Developer?</title>
		<link>http://www.jonkragh.com/index.php/iphone-development-a-bad-use-of-time-for-a-net-developer/</link>
		<comments>http://www.jonkragh.com/index.php/iphone-development-a-bad-use-of-time-for-a-net-developer/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 03:14:31 +0000</pubDate>
		<dc:creator>Jon Kragh</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.jonkragh.com/index.php/iphone-development-a-bad-use-of-time-for-a-net-developer/</guid>
		<description><![CDATA[OK, so even though I’m a “.NET Developer”, I’m really not just a “.NET Developer”, but at this point it is the technology stack that pays the bills around here, so lets just keep it at that for the sake of this post.
Recently I bought a refurbished MacBook Pro, that had all of the upgrades [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so even though I’m a “.NET Developer”, I’m really not just a “.NET Developer”, but at this point it is the technology stack that pays the bills around here, so lets just keep it at that for the sake of this post.</p>
<p>Recently I bought a refurbished MacBook Pro, that had all of the <a href="http://www.jonkragh.com/wp-content/uploads/2009/01/mbpro15old-av1.jpg"><img title="MBPRO15OLD_AV1" style="border-right: 0px; border-top: 0px; display: inline; margin: 5px 10px 0px 0px; border-left: 0px; border-bottom: 0px" height="244" alt="MBPRO15OLD_AV1" src="http://www.jonkragh.com/wp-content/uploads/2009/01/mbpro15old-av1-thumb.jpg" width="244" align="left" border="0" /></a>upgrades for the Feb 2008 Rev. (7200 RPM Drive, 2.6 Ghz Intel Peryn Dual Core, 6MB L2 Cache, 512 MB Video RAM, etc).&#160; My main use for this machine currently is for all of my personal software including Cubase SX 4.5 (an audio sequencer), loads of VST plugins, samples, pictures, videos, etc.&#160; In addition to running those apps, I can now run the iPhone SDK.</p>
<p>I have quite a few software product ideas right now, and some of them are iPhone apps, some of them are web based, and some are some cross platform desktop app ideas.&#160; </p>
<p>So without thinking logically about any of this, I started digging through the iPhone SDK and Developer Videos.&#160; I gave myself all kinds of reasons that this would be an ok use of my time, like “hey learning other languages makes you a better developer”, “hey seeing other architectures is good too”, “oh, I consider myself a software designer because I create applications that are well designed that people want to use so maybe I should learn more about Apple”, “ “this iPhone market doesn’t really seem too saturated, and I bet I could compete with a lot of leading apps”, on, and on, and on.</p>
<p>Then <strong>reality kicked in </strong>and I said to myself, WTF am I doing here?&#160; I’m going to spend my time managing memory in Objective C? I’m going to create an app that sells for a couple bucks and then hope that it sells how many apps to be somewhat profitable? Hmm, umm, and what can I use this Objective C experience for the next time I’m deep in ASP.net, WPF, javascript or CSS?</p>
<p>So I went from one extreme to the other and now I’m kind of sitting in this nice little pocket of gray where there is no clear answer.&#160; I figure I’m just going to finish up some web oriented features that I want to get out onto <a href="http://www.vastrank.com">Vast Rank</a> (internationalization is coming!), and kind of poke around with Cocoa, UIKit, Objective-C, and XTools for fun on the side.&#160; After I play around with the SDK for a little bit, I can see if I want to commit to taking on some of the iPhone app ideas I have.</p>
<p>Are you a .NET or Java developer considering developing an iPhone app?&#160; Do you think it is a good use of your time, if your app does not take off?&#160; I’m still on the fence right now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonkragh.com/index.php/iphone-development-a-bad-use-of-time-for-a-net-developer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
