<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' version='2.0'><channel><atom:id>tag:blogger.com,1999:blog-7699308385263147773</atom:id><lastBuildDate>Wed, 11 Mar 2009 21:36:12 +0000</lastBuildDate><title>code.nontalk.com</title><description>Code snippets for C#, T-SQL and JavaScript</description><link>http://code.nontalk.com/</link><managingEditor>noreply@blogger.com (nontalk)</managingEditor><generator>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-7699308385263147773.post-5946474892188800517</guid><pubDate>Fri, 19 Dec 2008 20:10:00 +0000</pubDate><atom:updated>2008-12-19T14:24:55.574-06:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>C#</category><title>Simple Html Email Template Class</title><description>&lt;p&gt;I very often have to send HTML formatted email addresses from a web application. I have taken many approaches when building the HTML message. Lately I have been using html files as templates which have replaceable &amp;quot;tokens&amp;quot;. Then I read the file into a string then do a series of replace statements like so:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;string name = "Jesse";
string city = "Minneapolis";

string body = FileHelper.Read("~/templates/email.htm");
body = body.Replace("{Name}", name);
body = body.Replace("{City}", city);

[...send email...]&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;After noticing how many times I have done this same thing, I decided to build a simple class to make this easier. My new code looks like this.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;string name = "Jesse";
string city = "Minneapolis";

Template t = new Template("~/templates/email.htm");
t.Tokens.Add("{Name}", name);
t.Tokens.Add("{City}", city);
string body = t.Replace();

[...send email...]&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's actually more lines of code but it 'feels' cleaner to me. Here's the class.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;using System;
using System.Collections.Generic;

public class Template
{
    public string Contents { get; set; }
    public Dictionary&lt;string, string&gt; Tokens { get; set; }

    public Template() {}

    public Template(string filename)
    {
        this.Tokens = new Dictionary&lt;string, string&gt;();
        this.Contents = FileHelper.Read(filename);
    }

    public Template(string contents, Dictionary&lt;string, string&gt; tokens)
    {
        this.Contents = contents;
        this.Tokens = tokens;
    }

 public string Replace()
 {
        foreach (KeyValuePair&lt;string, string&gt; item in this.Tokens)
        {
            this.Contents = this.Contents.Replace(item.Key, item.Value);
        }
        return this.Contents;
 }

    public static string GetReplacedFile(string filename, Dictionary&lt;string, string&gt; tokens)
    {
        Template t = new Template(filename);
        t.Tokens = tokens;
        return t.Replace();
    }

    public static string ReplaceTokens(string contents, Dictionary&lt;string, string&gt; tokens)
    {
        Template t = new Template(contents, tokens);
        return t.Replace();
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here's the FileHelper class that I use.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

/// &lt;summary&gt;
/// Provides utility methods for working with files.
/// &lt;/summary&gt;
public class FileHelper
{

 public static String ImageExtPattern
 {
  get { return @"png|jpg|jpeg|gif|tiff|tif"; }
 }

 public static string Read(String filename)
 {
  if (!Path.IsPathRooted(filename))
  {
   filename = System.Web.HttpContext.Current.Server.MapPath(filename);
  }
  return ReadFromDisk(filename);
 }

 private static string ReadFromDisk(String filename)
 {
  StringBuilder sb = new StringBuilder();
  StreamReader sr = File.OpenText(filename);
  String str = sr.ReadLine();

  while (str != null)
  {
   sb.Append(str + "\n");
   str = sr.ReadLine();
  }

  sr.Close();
  return sb.ToString();
 }

 public static void SaveFile(String directory, String filename, System.Web.HttpPostedFile file)
 {
  if (!Path.IsPathRooted(directory))
  {
   directory = HttpContext.Current.Server.MapPath(directory);
  }

  if (!Directory.Exists(directory))
  {
   Directory.CreateDirectory(directory);
  }

  String path = Path.Combine(directory, filename);

  file.SaveAs(path);
  file.InputStream.Close();
 }

 public static void DeleteFile(String path, String name)
 {
        if (string.IsNullOrEmpty(name)) return;
        string filePath = Path.Combine(path, name);
        DeleteFile(path);
 }

 public static void DeleteFile(String path)
 {
        if (string.IsNullOrEmpty(path)) return;
        if (!Path.IsPathRooted(path)) path = HttpContext.Current.Server.MapPath(path);
  if (File.Exists(path)) File.Delete(path);
 }


    public static Boolean Exists(String path, String name)
    {
        if (string.IsNullOrEmpty(name)) return false;
        string filePath = Path.Combine(path, name);
        return Exists(filePath);
    }

    public static Boolean Exists(String path)
 {
        if (string.IsNullOrEmpty(path)) return false;
        if (!Path.IsPathRooted(path)) path = HttpContext.Current.Server.MapPath(path);
  return File.Exists(path);
 }
}&lt;/code&gt;&lt;/pre&gt;</description><link>http://code.nontalk.com/2008/12/simple-template-class.html</link><author>noreply@blogger.com (nontalk)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-7699308385263147773.post-2218073817746820422</guid><pubDate>Tue, 24 Apr 2007 19:50:00 +0000</pubDate><atom:updated>2007-09-13T11:48:28.799-05:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>Regular Expressions</category><category domain='http://www.blogger.com/atom/ns#'>C#</category><title>Convert Relative Paths to Absolute Using Regular Expressions</title><description>&lt;p&gt;I ran into a situation where I needed to screen scrape some content from a site and display it on my own site. This works really well except for dependent files like javascripts, SWFs and images that had &lt;code&gt;src&lt;/code&gt; attributes with relative paths. So I figured it wouldn't be that hard to create a helper method to find and replace them using Regular Expressions. So here it is:&lt;/p&gt;  &lt;pre&gt;&lt;code&gt;public static String ConvertRelativePathsToAbsolute(String text, String absoluteUrl)
{
	String value = Regex.Replace(text,
		&amp;quot;&amp;lt;(.*?)(src|href)=\&amp;quot;(?!http)(.*?)\&amp;quot;(.*?)&amp;gt;&amp;quot;,
		&amp;quot;&amp;lt;$1$2=\&amp;quot;&amp;quot; + absoluteUrl + &amp;quot;$3\&amp;quot;$4&amp;gt;&amp;quot;,
		RegexOptions.IgnoreCase | RegexOptions.Multiline);
	
	// Now just make sure that there isn't a // because if
	// the original relative path started with a / then the
	// replacement above would create a //.

	return value.Replace(absoluteUrl + &amp;quot;/&amp;quot;, absoluteUrl);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sample Usage:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;String html = &amp;quot;&amp;lt;p&amp;gt;&amp;lt;img src=\&amp;quot;images/dot.gif\&amp;quot; alt=\&amp;quot;test\&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;&amp;quot;;
String baseUrl = &amp;quot;http//www.nontalk.com/&amp;quot;;
String replacedHtml = ConvertRelativePathsToAbsolute(html, baseUrl);
// replacedHtml =&amp;gt; &amp;lt;p&amp;gt;&amp;lt;img src=&amp;quot;http://www.nontalk.com/images/dot.gif&amp;quot; alt=\&amp;quot;test\&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt; &lt;/code&gt;&lt;/pre&gt; </description><link>http://code.nontalk.com/2007/04/convert-relative-paths-to-absolute.html</link><author>noreply@blogger.com (nontalk)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-7699308385263147773.post-6223393476058783495</guid><pubDate>Thu, 02 Nov 2006 23:12:00 +0000</pubDate><atom:updated>2006-11-02T18:17:56.513-06:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>javascript</category><category domain='http://www.blogger.com/atom/ns#'>workaround</category><category domain='http://www.blogger.com/atom/ns#'>css</category><title>CSS + Javascript Workaround for Styling Form Inputs in Internet Explorer (IE)</title><description>&lt;style type="text/css"&gt;
  #listing1 {
    border-collapse: collapse;
    margin-left: 2px;
  }
  #listing1 td {
    border: solid 1px #999;
    padding: .25em .75em;
  }
  .example {
    font-size: 150%;
    font-weight: bold;
  }
&lt;/style&gt;
&lt;p&gt;Checkboxes, textboxes, radio buttons and submit buttons all share the same HTML tag name &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt;. The &lt;code&gt;type&lt;/code&gt; attribute is what distinguishes each element from each other. For example:&lt;/p&gt;

&lt;table id="listing1"&gt;
  &lt;tr&gt;
    &lt;th&gt;Markup&lt;/th&gt;
    &lt;th&gt;Result&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;&amp;lt;input &lt;em&gt;type=&amp;quot;text&amp;quot;&lt;/em&gt; /&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;&amp;lt;input &lt;em&gt;type=&amp;quot;checkbox&amp;quot;&lt;/em&gt; /&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="checkbox" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;&amp;lt;input &lt;em&gt;type=&amp;quot;radio&amp;quot;&lt;/em&gt; /&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="radio" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;&amp;lt;input &lt;em&gt;type=&amp;quot;button&amp;quot;&lt;/em&gt; value=&amp;quot;button&amp;quot; /&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="button" value="button" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;CSS2 gives us the the ability to create style rules based on attributes of elements. So we could create a rule such as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;input[&lt;strong&gt;type='button'&lt;/strong&gt;] {
  border: dashed 2px green;
}
input[&lt;strong&gt;type='text'&lt;/strong&gt;] {
  border: solid 2px red;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would apply a dashed green border around all the buttons on a page and a solid red border around all text boxes. Unfortunately....you guessed it....Internet Explorer does not support attribute selectors in CSS. So if we want apply different styles to each type of input, then we either have to create inline styles like 
&lt;pre&gt;&lt;code&gt;&amp;lt;input type=&amp;quot;text&amp;quot; &lt;em&gt;style=&amp;quot;border: solid 2px red;&amp;quot;&lt;/em&gt; /&amp;gt;&lt;/code&gt;&lt;/pre&gt; or apply a class to each element and then create a corresponding CSS class definition.&lt;pre&gt;&lt;code&gt;&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;
  .textbox { border: solid 2px red; }
&amp;lt;/style&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; &lt;em&gt;class=&amp;quot;textbox&amp;quot;&lt;/em&gt; /&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Using inline styles is almost never a good idea, and adding a &lt;code&gt;class&lt;/code&gt; attribute on every form element is tedious, so I wanted to figure out a way to make this easy on all of us until the day when Microsoft decides to support CSS more fully.&lt;/p&gt;
&lt;p&gt;My idea was to create a JavaScript which would find each &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element on the page and add a class name corresponding to the type of &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; it is.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://code.nontalk.com/examples/css_inputs.htm" class="example" title="View an Example"&gt;View an Example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is the Javascript code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
    function addClassNamesToInputs() {
        var inputs = document.getElementsByTagName("input");
        for (var i=0; i&amp;lt;inputs.length; i++) {
            inputs[i].className = inputs[i].type;
        }
    }
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And here is the CSS which would go along with it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;style type="text/css"&amp;gt;
    body { font-family: sans-serif; font-size: 75%; }
    
    input {
        border: solid 2px red;
        }
    input.radio {
        border: none;
        }
    input.checkbox {
        border: none;
        width: 30px;
        height: 30px;
        }
    input.submit {
        border: solid 2px black;
        font-family: monospace;
        font-size: 18px;
        }
    input.reset {
        border: solid 2px green;
        font-family: Sans-Serif;
        font-size: 11px;
        font-weight: bold;
        }
    input.button {
        border: solid 2px blue;
        font-family: Serif;
        font-size: 25px;
        }
&amp;lt;/style&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you use the prototype.js library, you would want to use the &lt;code&gt;Element.addClassName()&lt;/code&gt; function in the script because, the script above will overwrite any existing class name&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
    function addClassNamesToInputs() {
        var inputs = document.getElementsByTagName("input");
        for (var i=0; i&amp;lt;inputs.length; i++) {
            inputs[i].addClassName(inputs[i].type);
        }
    }
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;</description><link>http://code.nontalk.com/2006/11/css-workaround-for-styling-form-inputs.html</link><author>noreply@blogger.com (nontalk)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>3</thr:total></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-7699308385263147773.post-2798962071556534458</guid><pubDate>Thu, 19 Oct 2006 21:47:00 +0000</pubDate><atom:updated>2006-10-21T18:16:06.158-05:00</atom:updated><category domain='http://www.blogger.com/atom/ns#'>javascript</category><title>Read/Modify Querystring Variables with Javascript</title><description>&lt;p&gt;There are a lot of &lt;a href="http://www.google.com/search?q=javascript+querystring"&gt;nice little javascripts out there&lt;/a&gt; which allow you to easily read values from the query string, but I wanted a script that would allow you to easily read &lt;em&gt;and&lt;/em&gt; modify querystring values.  I created the following script which relies on the &lt;a href="http://prototype.conio.net/"&gt;Prototype library&lt;/a&gt; (version 1.5.0_rc0):&lt;/p&gt;&lt;pre&gt;&lt;code&gt;/*--------------------------------------------------------------------------*/
/*  QueryString Object
*  (c) 2006 Jesse Gavin http://code.nontalk.com/
*
*  Depends upon:
*  - Prototype JavaScript framework, version 1.5.0_rc0
*    (c) 2005 Sam Stephenson &amp;lt;sam@conio.net&amp;gt;
*
*  This script is freely distributable under the terms of an MIT-style license.
*  For details, see: http://code.nontalk.com/
*
/*--------------------------------------------------------------------------*/

var QueryString = {
   params : $H(window.location.search.toQueryParams()),
  
   get : function(key, defaultValue) {
       if (defaultValue == null) defaultValue = null;
       var value = this.params[key]
       if (value==null) value = defaultValue;
       return value;
   },
  
   set : function(key, value) {
       this.params[key] = value;
   },
  
   remove : function(key) {
       this.params = this.params.collect(function(param) {
           if (key != param.key) return param;
       }).compact();
   },
  
   make : function() {
       return "?" + this.params.collect(function(param) {
           return escape(param.key) +"="+ escape(param.value);
       }).join("&amp;");
   },
  
   go : function() {
       window.location.href = location.pathname + this.make();
   }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://code.nontalk.com/QueryString/QueryString.js"&gt;Download QueryString.js&lt;/a&gt;&lt;/p&gt;&lt;p&gt;P.S. I got the idea for this from a &lt;a href="http://www.codeproject.com/aspnet/SimpleQueryString.asp"&gt;CodeProject article&lt;/a&gt; written by Uwe Keim. He created a C# class to manage QueryString variables, which does all this and more on the server side.&lt;/p&gt;</description><link>http://code.nontalk.com/2006/10/readmodify-querystring-variables-with.html</link><author>noreply@blogger.com (nontalk)</author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></item></channel></rss>