HtmlEncode: Fix Problems with certain characters

Posted on: Wed Sep 24 11:06:04 -0700 2008. Updated on: Wed Sep 24 11:08:57 -0700 2008.
Category: DotNet

Have you ever had a form with a few fields where users can enter data, and realize that apostrophes give you errors?

This is where Server.HtmlEncode and Server.HtmlDecode come in very handy. Not only does this fix apoostrophes but other characters that need to be stored as html in the database so that they display correctly on their way out. Also avoid problems when trying to write to the database.

string encodedTextForDb = Server.HtmlEncode(TextBoxName.Text);

// When you want to display the text coming out of your db storage correctly:

LabelName.Text = Server.HtmlDecode(dataTable.Name);

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.