Display special symbols and HTML code in your ASP.NET page

Hi,

If you try to add the following examples to your site you will not get the expected result:

Enter your PIN <here>

You need to use <a> tag to create a hyperlink.

They don’t give the expected result because ASP.NET tries to convert this to something it knows. It sees it a tag and thinks it needs to convert that. This is basically an encoding problem.  You need to convert the <,> symbols to something that HTML can encode as a regular  < or > and not a full tag. This is done using the Server.HtmlEncode() function.

You can solve the problem this way:

protected void Page_Load(object sender, EventArgs e) { lblResult.Text = Server.HtmlEncode("Enter your PIN <here>"); lblResult.Text += "<br />" + Server.HtmlEncode("You need to use the <a> tag to create a hyperlink."); }


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word