ASP.NET MVC: Charting Configuration and Example

As far as I know this is the minimum you need to get charting up and running under ASP.NET MVC 2/3. This works with the ASP ViewEngine only. This configuration is using the .NET 4 version of the libraries but it works with 3.5 as well. You would need to update the version numbers accordingly.

Global.asax
(Kept getting a 404 looking for ChartImg.axd until I added this)

public static void RegisterRoutes(RouteCollection routes)
{
    ...
    RouteTable.Routes.Ignore("{*pathInfo}", new { pathInfo = @"^.*(ChartImg.axd)$" });
    ...
}

Web.config
<system.web>
    <pages>
        <controls>
            ...
            <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            ...
        </controls>
    </pages>
    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            ...
            <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            ...
        </assemblies>
    </compilation>    
    <httpHandlers>
        ...
        <add verb="*" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>   
        ...
    </httpHandlers>
</system.web>

View
<%@ Import Namespace="System.Web.UI.DataVisualization.Charting" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <%
            var Chart2 = new Chart();
            Chart2.Width = 412;
            Chart2.Height = 296;
            Chart2.RenderType = RenderType.ImageTag;

            Chart2.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.BrightPastel;
            Title t = new Title("No Code Behind Page", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            Chart2.Titles.Add(t);
            Chart2.ChartAreas.Add("Series 1");

   // create a couple of series
            Chart2.Series.Add("Series 1");
            Chart2.Series.Add("Series 2");
            
            // you can replace these and store them in viewdata or your own class (or build the whole control in the viewdata and pass it back to the page
            int[] series1 = { 1, 2, 3, 2, 1, 2, 3 };
            int[] series2 = { 3, 2, 1, 2, 3, 2, 1 };

            // add points to series 1
            foreach (int value in series1)
            {
                Chart2.Series["Series 1"].Points.AddY(value);
            }

            // add points to series 2
            foreach (int value in series2)
            {
                Chart2.Series["Series 2"].Points.AddY(value + 1);
            }

            Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            Chart2.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
            Chart2.BorderlineDashStyle = ChartDashStyle.Solid;
            Chart2.BorderWidth = 2;

            Chart2.Legends.Add("Legend1");

   // Render chart control
            Chart2.Page = this;
   HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);
   Chart2.RenderControl(writer);
        %>
</asp:Content>

jQuery: Module Stub

jQuery module stub with basic namespace and function declarations. Not sure if this is the right way to do it. YMMV.

// usage $("selector").moduleStub(options);
(function ($) {
    $.fn.moduleStub = function (options) {
        return inlineEditor.init(this, options);
    };
})(jQuery);

var moduleStub = {
    el: {},
    options: {},
    init: function (el, options) {
        this.el = $(el);
        if (options) { $.extend(this.options, options); }
        return this;
    },
    test: function () {
        alert(this.el.attr("id"));
    }
}

ASP.NET: #if DEBUG not working / still runs in production

Debug code still running in production after building in RELEASE mode (I think this is a msbuild problem)... Example:
#if DEBUG
//blah blah
#end if
Solution:
Switch to using something like this:
public static bool IsDevelopment
{
    get
    {
        return HttpContext.Current.IsDebuggingEnabled;
    }
}
This method uses the web.config defined attribute instead (configuration/system.web/compilation[debug="true"] attribute).

Windows 7: Search for folders by name

Problem:
You search for a folder by name and get all files and folders with that term anywhere in the path. What you really want is a list of folders that you can blow away in one shot, such as hidden ".svn" folders...

Solution:
kind:folder foldername:.svn

StringTemplate 4 .NET C# - Getting Started

Here's the basic idea for rendering a template in C#. String Template has weak .NET/C# documentation from what I can tell.

Resources
Visual Studio Project References
  • Antlr3.Runtime.dll
  • Antlr4.StringTemplate.dll
C# Code
Returns template output.
using System;
using System.IO;
using Antlr4.StringTemplate;

public namespace YourNamespace
{
 public class Tester
 {
  public string HelloWorld()
  {
   Template template;

   var path = @"C:\templates\template.st";
   var file = new FileInfo(path);
   using (StreamReader reader = file.OpenText())
    template = new Template(reader.ReadToEnd(), '$', '$'); // custom delimeter (defaults are "<" and ">")

   template.Add("string", "Hello World!");
   template.Add("array", new string[] { "Record A", "Record B", "Record C" });
   template.Add("object", new { PropertyA = "A", PropertyB = "B" });

   return template.Render();
  }
 }
}
Template File
$! file: C:\templates\template.st !$
String: 
 $string$

Array:
 $array:{it|$it$ ($i$)};separator=", "$

Object:
 Property A = $object.PropertyA$
 Property B = $object.PropertyB$

Gotchas
Upgrading from 3.2 to 4 today has been an unpleasant experience.
  • Curly braces need to be escaped with a backslash within loops, I don't remember having to do this before.
    $items:{item|
    \t{ ... item.property ... \}
    }$
    
  • Speaking of loops, the "it" alias no longer works. You have to alias them every time now.
    Good:
    $items:{it| $it.property$ }$
    Bad:
    $items:{ $it.property$ }$
  • This was likely invalid before but you can't put your $ variables in if statements anymore.
    Good:
     $if(mybool)$ ... $endif$
    Bad:
     $if($mybool$)$ ... $endif$

IIS: A connection was successfully established with the server, but then an error occurred during the pre-login handshake

Problem:
Server Error in '/' Application.


A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The client and server cannot communicate, because they do not possess a common algorithm.)

Solution:
Make sure your web.config has Persist Security Info=True. I don't know or care what the implications are if it's set this way so don't judge. I'm a developer, I just want things to work.

Flash: Dynamically Load Image in ActionScript 3.0

  1. Create image placeholder movie
  2. Add image placeholder movie on stage, name it "image_placeholder"
  3. Click on the first frame of the default layer and add the following:
    var myLoader:Loader = new Loader();
    myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);
    
    var fileRequest:URLRequest = new URLRequest("photo.jpg");
    myLoader.load(fileRequest);
    
    function onProgressStatus(e:ProgressEvent) {   
          trace(e.bytesLoaded, e.bytesTotal); 
    }
    
    function onLoaderReady(e:Event) {     
          image_placeholder.addChild(myLoader);
    }
    
    stop();