Feb
25
2009

New Engine, New Skin…

Knowing that my version of BlogEngine.NET was way out of date, I upgraded to the more recent and decided to throw on a new skin… I know Mads is probably going to drop the latest and greatest version 1.5 any day now right….

Enjoy, more to come… You can also follow me on Twittter as well!

Feb
3
2009

Basic C# Extension Methods: HtmlEncode and HtmlDecode

Extension methods… They’re cool, they’re great, and we all know that they are dangerous…. But….

Current project has a few forms, as always, and I found myself coding and decoding text input data, keeping a secure site, and it dawned on me…

 

Why don’t we use the features which were so graciously created for us??? Duh!

So here is a down and dirty Extension method to HtmlEncode and HtmlDecode a string…

 

public static string HtmlEncode(this string value)
{
  return HttpContext.Current.Server.HtmlEncode(value);
}
public static string HtmlDecode(this string value)
{
   return HttpContext.Current.Server.HtmlDecode(value);
}

If we are populating a form from our application model object, let’s just say a customer, it is easily implemented like so….

FirstNameLabel.Text = customer.FirstName.HtmlDecode();
LastNameLabel.Text = customer.LastName.HtmlDecode();


Or, if we are grabbing data from a form and populating our same application model customer object….

var newCustomer = new Customer
      {
           FirstName =  FirstNameTextBox.Text.HtmlEncode(),   
           LastName = LastName TextBox.Text.HtmlEncode()
      };

Enjoy,

RA

About me...

I am a software dev specializing in web based application lovin life in So Cal!

Month List