<?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>Musings&#60;Biefeld&#62; &#187; Asp.net</title>
	<atom:link href="http://sbiefeld.com/category/coding/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://sbiefeld.com</link>
	<description>- curiosities of development, life, the universe and everything -</description>
	<lastBuildDate>Thu, 17 Nov 2011 04:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Serving Images from an Image Controller</title>
		<link>http://sbiefeld.com/2011/09/serving-images-from-an-image-controller/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=serving-images-from-an-image-controller</link>
		<comments>http://sbiefeld.com/2011/09/serving-images-from-an-image-controller/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 16:02:54 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=432</guid>
		<description><![CDATA[Found this solution rather simple when you want to serve images from your database. public class ImageController { readonly IImageQueries Queries; public ImageController(IImageQueries queries) { Queries = queries; } public FileResult Show(int id) { DateTime? dateCreated = Queries.GetDateCreatedById(id); //invalid image, return 404 if (dateCreated == null) { HttpContext.Response.StatusCode = 404; return null; } else { [...]]]></description>
			<content:encoded><![CDATA[<p>Found this solution rather simple when you want to serve images from your database.</p>
<pre style="overflow-x:scroll;background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333;overflow: auto;color: #BEBEC8;font-size: 8pt">
<span style="color: #cda869">public</span> <span style="color: #7386a5">class</span> <span style="color: #7386a5">ImageController</span>
{
	<span style="color: #cda869">readonly</span> <span style="color: #d0da90">IImageQueries</span> Queries;

	<span style="color: #cda869">public</span> ImageController(<span style="color: #d0da90">IImageQueries</span> queries)
	{
		Queries <span style="color: #cda869">=</span> queries;
	}

	<span style="color: #cda869">public</span> FileResult Show(int id)
	{
		<span style="color: #7386a5">DateTime</span><span style="color: #cda869">?</span> dateCreated <span style="color: #cda869">=</span> Queries<span style="color: #cda869">.</span>GetDateCreatedById(id);

		<span style="color: #444">//invalid image, return 404</span>
		<span style="color: #cda869">if</span> (dateCreated <span style="color: #cda869">== null</span>)
		{
			<span style="color: #7386a5">HttpContext</span><span style="color: #cda869">.</span>Response<span style="color: #cda869">.</span>StatusCode <span style="color: #cda869">=</span> 404;

			<span style="color: #cda869">return null</span>;
		}
		<span style="color: #cda869">else</span>
		{
			<span style="color: #cda869">string</span> ifModifiedSince = <span style="color: #7386a5">HttpContext</span><span style="color: #cda869">.</span>Request<span style="color: #cda869">.</span>Headers[<span style="color: #8f9d6a">"If-Modified-Since"</span>];

			<span style="color: #444">//check if image has been modified</span>
			<span style="color: #cda869">if</span> (ifModifiedSince <span style="color: #cda869">!= null &#038;&#038;</span> dateCreated.Value <span style="color: #cda869">&lt;=</span> <span style="color: #7386a5">DateTime</span><span style="color: #cda869">.</span>Parse(ifModifiedSince))
			{
				<span style="color: #7386a5">HttpContext</span><span style="color: #cda869">.</span>Response<span style="color: #cda869">.</span>StatusCode <span style="color: #cda869">=</span> 304;
				<span style="color: #cda869">return null</span>;
			}
			<span style="color: #cda869">else</span>
			{
				<span style="color: #7386a5">HttpContext</span><span style="color: #cda869">.</span>Response<span style="color: #cda869">.</span>Cache<span style="color: #cda869">.</span>SetLastModified(dateCreated<span style="color: #cda869">.</span>Value);

				<span style="color: #cda869">dynamic</span> image = Queries<span style="color: #cda869">.</span>GetById(id);

				<span style="color: #cda869">return new</span> FileContentResult(image<span style="color: #cda869">.</span>Bytes, image<span style="color: #cda869">.</span>MimeType);
			}
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2011/09/serving-images-from-an-image-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic View Page, MVC without a View Model</title>
		<link>http://sbiefeld.com/2011/09/dynamic-view-page-mvc-without-a-view-model/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dynamic-view-page-mvc-without-a-view-model</link>
		<comments>http://sbiefeld.com/2011/09/dynamic-view-page-mvc-without-a-view-model/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 16:19:07 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=427</guid>
		<description><![CDATA[Hello fellow techies on this roller coaster called life. It has been a while since my last post, I have been focusing on some other more important aspects of my life. What I want to talk about in this post is the idea of a dynamic view page which removes the need for a View [...]]]></description>
			<content:encoded><![CDATA[<p>Hello fellow techies on this roller coaster called life. It has been a while since my last post, I have been focusing on some other more important aspects of my life.</p>
<p>What I want to talk about in this post is the idea of a dynamic view page which removes the need for a View Model data transfer object. I have started to try this out on project that I am working on, for views that are essentially read only. All these views are concerned about is displaying data/information.  I do not yet know how useful this is but I thought it was novel and will be exploring it&#8217;s usefulness. Plus, the lazy side of me likes the idea of not creating data transfer objects if possible.</p>
<p>Two side effects of this approach are the loss of the strongly typed Model on your view, which is a non-issue for a page without inputs.  The second is the loss of intellisense (the very first time a member is used on a dynamic) when working with the Model in the view, which could be a minor loss in that you wont know something is wrong until you run it, but thats the way it goes.</p>
<p>To accomplish this you start off by creating a DynamicViewPage that inherits ViewPage.  Have some overrides and new up some properties and you should be set.</p>
<pre><span style="color: #cda869;">public class</span> <span style="color: #7386a5;">DynamicViewPage</span> : <span style="color: #7386a5;">ViewPage</span>
{
	<span style="color: #cda869;">private</span> <span style="color: #7386a5;">ViewDataDictionary</span><span style="color: #cda869;">&lt;dynamic&gt;</span> _viewData;

	<span style="color: #cda869;">public override void</span> InitHelpers()
	{
		<span style="color: #cda869;">base.</span>InitHelpers();
		Ajax = <span style="color: #cda869;">new</span> <span style="color: #7386a5;">AjaxHelper</span><span style="color: #cda869;">&lt;dynamic&gt;</span>(ViewContext, this);
		Html = <span style="color: #cda869;">new</span> <span style="color: #7386a5;">HtmlHelper</span><span style="color: #cda869;">&lt;dynamic&gt;</span>(ViewContext, this);
	}

	<span style="color: #cda869;">protected override void</span> SetViewData(<span style="color: #7386a5;">ViewDataDictionary</span> viewData)
	{
		_viewData = new <span style="color: #7386a5;">ViewDataDictionary</span><span style="color: #cda869;">&lt;dynamic&gt;</span>(viewData);
		<span style="color: #cda869;">base.</span>SetViewData(_viewData);
	}

	<span style="color: #cda869;">public new</span> <span style="color: #7386a5;">AjaxHelper</span><span style="color: #cda869;">&lt;dynamic&gt;</span> Ajax { get; set; }

	<span style="color: #cda869;">public new</span> <span style="color: #7386a5;">HtmlHelper</span><span style="color: #cda869;">&lt;dynamic&gt;</span> Html { get; set; }

	<span style="color: #cda869;">public new dynamic</span> Model
	{
		<span style="color: #cda869;">get</span>
		{
			<span style="color: #cda869;">return</span> ViewData.Model;
		}
	}

	<span style="color: #cda869;">public new</span> <span style="color: #7386a5;">ViewDataDictionary</span><span style="color: #cda869;">&lt;dynamic&gt;</span> ViewData
	{
		<span style="color: #cda869;">get</span>
		{
			<span style="color: #cda869;">if</span> (_viewData <span style="color: #cda869;">== null</span>)
			{
				SetViewData(new <span style="color: #7386a5;">ViewDataDictionary</span><span style="color: #cda869;">&lt;dynamic&gt;</span>());
			}
			<span style="color: #cda869;">return</span> _viewData;
		}
		<span style="color: #cda869;">set</span>
		{
			SetViewData(<span style="color: #cda869;">value</span>);
		}
	}
}</pre>
<p>Now your view needs to inherit from DynamicViewPage.</p>
<pre>Inherits="Web.Views.DynamicViewPage"</pre>
<p>In your controller you can ask your persistence abstraction of choice for some data and return it to the view.</p>
<pre><span style="color: #cda869;">public</span> <span style="color: #7386a5;">ActionResult</span> Index()
{
	<span style="color: #d0da90;">IEnumerable</span><span style="color: #cda869;">&lt;dynamic&gt;</span> foos <span style="color: #cda869;">=</span> FooQueries.GetAll();
	<span style="color: #cda869;">return</span> View(foos);
}</pre>
<p>Where this can get interesting is when you have a linq provider that returns dynamic objects. Then essentially your domain model is king and you eliminate a lot of DTOs. That&#8217;s all I&#8217;ve got for now. I attached a sample application that demonstrates the dynamic view, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2011/09/dynamic-view-page-mvc-without-a-view-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the asp.net textbox to have an OnBlur server side event.</title>
		<link>http://sbiefeld.com/2008/09/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extending-the-aspnet-textbox-to-have-an-onblur-server-side-event</link>
		<comments>http://sbiefeld.com/2008/09/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 20:34:17 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[Asp.net]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=22</guid>
		<description><![CDATA[I recently needed a textbox that would fire a server event when the textbox’s onblur JavaScript event was fired.&#160; It took a while of googling and asking coworkers questions to figure out how exactly to do it.&#160; Couldn’t seem to find an example of exactly what I needed. &#160; I started off by creating a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed a textbox that would fire a server event when the textbox’s <i>onblur</i> JavaScript event was fired.&#160; It took a while of googling and asking coworkers questions to figure out how exactly to do it.&#160; Couldn’t seem to find an example of exactly what I needed.
<p>&#160;</p>
<p>I started off by creating a class that inherited from textbox and implemented <i>ICallBackEventHandler.&#160; </i>I soon realized that this was not going to work for what I needed.&#160; The <i>ICallBackEventHandler</i> is best suited for a simple Ajax control that just needs to update its own content.&#160; Problem is that I need a post back to happen in order to update other controls on the page.</p>
<p>&#160;</p>
<p>So instead of implementing the <i>ICallBackEventHandler</i> interface I implemented the <i>IPostBackEventHanlder</i> interface.&#160; This turned out to be a much simpler, cleaner way to do it. </p>
<p>When implementing the <i>IPostBackEventHandler</i> you must Implement the <i>RaisePostBackEvent</i> method:</p>
<pre><span class="keyword">public</span> <span class="keyword">class</span> <span class="class">BlurTextbox</span> : <span class="class">TextBox, IPostBackEventHandler</span>
{
    <span class="keyword">#region</span> Implementation of IPostBackEventHandler

    <span class="comments">/// &lt;summary&gt;</span>
    <span class="comments">/// When implemented by a class, enables a server control to process an event raised when a form is posted to the server.</span>
    <span class="comments">/// &lt;/summary&gt;</span>
    <span class="comments">/// &lt;param name=&quot;eventArgument&quot;&gt;A &lt;see cref=&quot;T:System.String&quot; /&gt; that represents an optional event argument to be passed to the event handler. &lt;/param&gt;</span>
    <span class="keyword">public</span> <span class="keyword">void</span> RaisePostBackEvent(<span class="keyword">string</span> eventArgument)
    {

    }

    <span class="keyword">#endregion</span>
}</pre>
<p>&#160;</p>
<p>This method is what gets called by .net’s JavaScript when your control does a post back.</p>
<p>&#160;</p>
<p>Now what we want to do is create the JavaScript function that will cause the post back will allow the <i>RaisePostBackEvent</i> method to handle the logic for that event.</p>
<p>&#160;</p>
<p>I just created a method on the textbox that returns the script.&#160; You can use the Page.ClientScript.GetPostBackEventReference<i>(Control control, string argument)</i> method but this is not necessary because all the it generates is <i>__doPostBack(&#8216;controlName&#8217;,'argument&#8217;),</i> either way works.</p>
<p>&#160;</p>
<p>If you want to be able to have multiple instances of the control on the same page you need to write it yourself, this is what I did: </p>
<pre><span class="keyword">private</span> <span class="keyword">string</span> GetScript()
{
    <span class="keyword">return</span> <span class="string">&quot;function OnBlurred(control, arg)\n{\n __doPostBack(control, arg);\n}&quot;</span>;
}</pre>
<p>There are two parameters needed, the control and the argument, the control string that the asp.net JavaScript call is expecting will need the textbox’s <i>UniqueID</i> for it to work properly.&#160; </p>
<p>Next I created a server side event on the control called Blur:</p>
<pre><span class="keyword">public</span> <span class="keyword">delegate</span> <span class="keyword">void</span> <span class="class">OnBlurDelegate</span>(<span class="keyword">object</span> sender, <span class="class">EventArgs</span> e);

<span class="keyword">public</span> <span class="keyword">event</span> <span class="class">OnBlurDelegate</span> Blur;

<span class="keyword">private</span> <span class="keyword">void</span> RaiseOnBlurEvent()
{
    <span class="keyword">if</span>(Blur != <span class="keyword">null</span>)
    {
        Blur(<span class="keyword">this</span>, <span class="class">EventArgs</span>.Empty);
    }
}</pre>
<p>Now we can call the <i>RaiseOnBlurEvent()</i> method in the implemented <i>RaisePostBackEvent()</i> method like:</p>
<pre><span class="keyword">public</span> <span class="keyword">void</span> RaisePostBackEvent(<span class="keyword">string</span> eventArgument)
{
    RaiseOnBlurEvent();
}</pre>
<p>Lastly I overrode the <i>OnInit</i> event, called the base.&#160; I check if that script is already registered, if it’s not I register it, otherwise move one.&#160; Then registered the <i>OnBlurred</i> script we created, and added the JavaScript <i>onblur</i> event attribute to the textbox.&#160; I pass in the <i>UniqueID</i> and empty quotes because I don’t need any arguments.</p>
<pre><span class="keyword">protected</span> <span class="keyword">override</span> <span class="keyword">void</span> OnInit(EventArgs e)
{
    <span class="keyword">base</span>.OnInit(e);
    <span class="keyword">if</span> (!Page.ClientScript.IsClientScriptBlockRegistered(<span class="string">&quot;OnBlurTextboxEvent&quot;</span>))
        Page.ClientScript.RegisterStartupScript(GetType(), <span class="string">&quot;OnBlurTextboxEvent&quot;</span>, GetScript(), <span class="keyword">true</span>);
    Attributes.Add(<span class="string">&quot;onblur&quot;</span>, <span class="string">&quot;OnBlurred('&quot;</span> + UniqueID + <span class="string">&quot;','')&quot;</span>);
}</pre>
<p>Now all that is left to do is use it on some web page and setup the server side handling.</p>
<p>Aspx:</p>
<pre>&lt;<span class="keyword">cc3:BlurTextbox</span> <span class="class">ID</span>=&quot;Test&quot; <span class="class">runat</span>=&quot;server&quot; <span class="class">OnBlur</span>=&quot;Test_OnBlur&quot;<span class="keyword">&gt;&lt;/</span><span class="keyword">cc3:BlurTextbox</span><span class="keyword">&gt;</span></pre>
<p>Server Handling:</p>
<pre><span class="keyword">protected</span> <span class="keyword">void</span> Test_OnBlur(<span class="keyword">object</span> sender, <span class="class">EventArgs</span> e)
{
}</pre>
<p>The whole thing is <a href="http://sbiefeld.com/Stuff/BlurTextbox.cs">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2008/09/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

