Difference between modes of literal control

2019-04-24 00:39发布

问题:

What is the difference between the passthrough and Transform modes of literal control?

Could you post an example, too?

回答1:

There are different Literal Modes Literal.Mode

  1. PassThrough : The contents of the control are not modified.
  2. Encode : The contents of the control are converted to an HTML-encoded string.
  3. Transform : Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

Have a look at this MSDN article http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.mode.aspx

and take a look at this implemented example Use ASP.NET's Literal control to its full potential



回答2:

If you decompile System.Web.UI.WebControls.Literal.Render, you get this:

protected internal override void Render(HtmlTextWriter writer)
{
    string text = this.Text;
    if (text.Length != 0)
    {
        if (this.Mode != LiteralMode.Encode)
        {
            writer.Write(text);
        }
        else
        {
            HttpUtility.HtmlEncode(text, writer);
        }
    }
}

This is the same for .NET 2.0 and .NET 4.0.

So whatever the documentation says, there is no difference between Transform (default) and PassThrough.

Please correct me if I'm wrong. There are plenty of articles that just repeat the official documentation, but I would like to see a code sample that proves that there is a difference.