ASP.NET Tutorial/Development/Html Encode

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Common HTML Special Characters

   <source lang="csharp">

Result Description Encoded Entity

          Nonbreaking space         

< Less-than symbol < > Greater-than symbol > & Ampersand & " Quotation mark "</source>

HTML encoding test (C#)

   <source lang="csharp">

File: Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HtmlEncodeTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"


<html xmlns="" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form ID="form1" runat="server">

Properly encoded:

       <div ID="ctrl2" runat="server"/>

Incorrectly encoded:

       <div ID="ctrl1" runat="server"/>

</body> </html>

File: Default.aspx.cs

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class HtmlEncodeTest : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   ctrl1.InnerHtml = "To bold text use the  tag.";
   ctrl2.InnerHtml = "To <b>bold text use the " + Server.HtmlEncode("") + " tag.";