93
Image: c http://www.flickr.com/photos/kenmccown/220565863 Wrestling with ASP.NET and Web Standards Anthony Williams

The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Embed Size (px)

Citation preview

Page 1: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/kenmccown/220565863

Wrestling with ASP.NET and Web

StandardsAnthony Williams

Page 2: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c Me

Hello!

Page 3: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: © Listers Group. Used with permission.

Work for a Car Dealer Group

Page 4: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: © Listers Group. Used with permission.

Work for a Car Dealer Group

I Work Here

Page 5: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: © http://www.bbc.co.uk/comedy/fastshow/wallpaper/images/swiss_1024.jpg

NOT a used car salesman

Page 6: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: © Microsoft

WindowsIIS

SQL ServerASP.NET

Page 7: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/planettelex/826864344

ASP ≠ ASP.NET

Page 8: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/wheatfields/515063339

Web Standards+

ASP.NET

Page 9: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/paix_et_amour/2398468615

Before we start...

Page 10: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/paix_et_amour/2398468615

★ .NET 3.5★ C#★ XHTML 1.0 Transitional

Page 11: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

1

Image: c http://www.flickr.com/photos/z287marc/3189567558

Out of the box

Page 12: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<%@ Page Language="C#" AutoEventWireup="true"    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        </div>    </form></body></html>

Page 13: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCHkJbOA8HXUlbSk8AyRMGmtOpja" /></div>    <div>        </div>    </form></body></html>

Page 14: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCHkJbOA8HXUlbSk8AyRMGmtOpja" /></div>    <div>        </div>    </form></body></html>

Page 15: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/mikahiironniemi/76988191

XHTML Conformance

Page 16: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<system.web>      <XhtmlConformance mode="Transitional" /></system.web>

Page 17: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<system.web>      <XhtmlConformance mode="Strict" /></system.web>

mode="Strict"

Page 18: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<system.web>      <XhtmlConformance mode="Strict" /></system.web>

Page 19: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<%@ Page Language="C#" AutoEventWireup="true"    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        </div>    </form></body></html>

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐transitional.dtd">

Page 20: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<%@ Page Language="C#" AutoEventWireup="true"    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        </div>    </form></body></html>

Page 21: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCHkJbOA8HXUlbSk8AyRMGmtOpja" /></div>    <div>        </div>    </form></body></html>

Page 22: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>    <form name="form1" method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCHkJbOA8HXUlbSk8AyRMGmtOpja" /></div>    <div>        </div>    </form></body></html>

Won’t Validate

Page 23: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!DOCTYPE html PUBLIC "‐//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1‐strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>

</title></head><body>    <form method="post" action="Default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCHkJbOA8HXUlbSk8AyRMGmtOpja" /></div>    <div>        </div>    </form></body></html>

Page 24: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<system.web>      <XhtmlConformance mode="Legacy" /></system.web>

mode="Legacy"

Page 25: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<system.web>      <XhtmlConformance mode="Legacy" /></system.web>

Page 26: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

2

Image: c http://www.flickr.com/photos/beatxm/2535486395

<asp:repeater />

Page 27: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:repeater id="rptNavigationLinks" runat="server">    <headertemplate>        <ul id="MainNavigation">    </headertemplate>    <itemtemplate>            <li <%# Eval("Link", "class=\"{0}\"") %>>                <a href="<%# Eval("LinkUrl") %>"><% Eval("LinkTitle") %></a>            </li>    </itemtemplate>    <footertemplate>        </ul>    </footertemplate></asp:repeater>

Page 28: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<ul id="MainNavigation">    <li class="Home">        <a href="/">Home</a>    </li>    <li>        <a href="/New">New Cars</a>    </li>    <li>        <a href="/Used">Used Cars</a>    </li>    <li>        <a href="/Jobs">Jobs</a>    </li>    <li>        <a href="/About">About Us</a>    </li></ul>

Page 29: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:repeater id="rptNavigationLinks" runat="server">    <headertemplate>        <table id="DataTable">            <thead>                <tr>                    <th> ... </th>                    <th> ... </th>                </tr>            </thead>            <tbody>    </headertemplate>    <itemtemplate>                <tr>                    <td> ... </td>                    <td> ... </td>                </tr>    </itemtemplate>    <footertemplate>            </tbody>        </table>    </footertemplate></asp:repeater>

Page 30: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<table id="DataTable">    <thead>        <tr>            <th>Letter</th>            <th>Phonetic</th>        </tr>    </thead>    <tbody>        <tr>            <td>A</td>            <td>Alpha</td>        </tr>        <tr>            <td>B</td>            <td>Bravo</td>        </tr>        <tr>            <td>C</td>            <td>Charlie</td>        </tr>    </tbody></table>

Page 31: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

3

Image: http://users.upwardaccess.com/plong/images/btvserver_treadmill.jpg

runat="server"

Page 32: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

visible="false"

Page 33: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 34: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 35: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 36: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 37: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 38: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 39: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/antonystanley/3093398360

Page 40: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

4

Image: c http://www.flickr.com/photos/wolfsoul/1368661325

Blank Alt Text

Page 41: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:image runat="server" id="Image1"           imageurl="~/accept.png" />

Page 42: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" />

Page 43: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" />style="border‐width:0px;"

Page 44: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" />

Page 45: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:image runat="server" id="Image1"      imageurl="~/accept.png"      alternatetext="" />

Page 46: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" />alt="" />

Page 47: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" />

Page 48: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:image runat="server" id="Image1"      imageurl="~/accept.png"      alternatetext=""      generateemptyalternatetext="true" />

Page 49: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:image runat="server" id="Image1"      imageurl="~/accept.png"      alternatetext=""      generateemptyalternatetext="true" />generateemptyalternatetext="true"

Page 50: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<asp:image runat="server" id="Image1"      imageurl="~/accept.png"      alternatetext=""      generateemptyalternatetext="true" />

Page 51: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" alt="" />alt="" />

Page 52: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png"      style="border‐width:0px;" alt="" />

Page 53: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<img id="Image1" src="accept.png" alt=""     runat="server" />

Page 54: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!‐‐ Original HTML ‐‐><img id="Image1" src="accept.png" />

Page 55: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!‐‐ Original HTML ‐‐><img id="Image1" src="accept.png" />

// Add this to your code‐behind (C#)Image1.Attributes.Add("alt", "");

Page 56: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<!‐‐ Original HTML ‐‐><img id="Image1" src="accept.png" />

// Add this to your code‐behind (C#)Image1.Attributes.Add("alt", "");

<!‐‐ HTML after additional code‐behind ‐‐><img id="Image1" src="accept.png" alt="" />

Page 57: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

// You’ll need .NET 3.0 (but seriously, use 3.5) for this to work, as well as needing to import// (using) the System.Linq and System.Collections.Generic namespaces in all code files that use this. 

// Put the ControlHelper class somewhere in your solution ‐ not necessarily your page:public static class ControlHelper{      public static IEnumerable<Control> AllControls(this ControlCollection Controls)      {            foreach (Control CurrentControl in Controls)            {                  foreach(var ChildControls in CurrentControl.Controls.AllControls())                  {                        yield return ChildControls;                  }                  yield return CurrentControl;            }      }}

// Then run this code in your pages that have img elements with runat="server" attributes:IEnumerable<HtmlImage> ImageControls =       Page.Controls          .AllControls()          .OfType<HtmlImage>()          .Where(r => r.Attributes["alt"] == null);

foreach (HtmlImage ImageControl in ImageControls) ImageControl.Attributes.Add("alt", "");

Page 58: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

5

Image: c http://www.flickr.com/photos/barnett/2836828090

Dealing with Submissions

Page 59: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/kristiand/3223920178

Content Editors

Page 60: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Sanitise user input

Image: c http://www.flickr.com/photos/herrolm/2699835587

Page 61: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Sanitise user input

Image: c http://www.flickr.com/photos/herrolm/2699835587

c http://xkcd.com/327/

Page 62: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Page 63: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Page 64: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Exception Details: System.Web.HttpRequestValidationException

Page 65: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

!"#$%&'($)&*+,&-(.(/0(1-234&*1(3$"5!"#$%&'($)&*+,&-(.(/0(1-234&*1(3$"5

Page 66: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

!"#$%&'($)&*+,&-(.(/0(1-234&*1(3$"5!"#$%&'($)&*+,&-(.(/0(1-234&*1(3$"5

Page 67: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

!"#"$%&'(%)*

++++++!,-.%"+/-012-$%3%45%"$678-0"%7+9*

!9"#"$%&'(%)*

!"#"$%&'(%)*

++++++!,-.%"+/-012-$%3%45%"$678-0"%7+9*

!9"#"$%&'(%)*

Page 68: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Page 69: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Server.HtmlEncode

Page 70: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Server.HtmlEncodeString.Replace()

Page 71: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/herrolm/2699835587

Server.HtmlEncodeString.Replace()

Regular Expressions

Page 72: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<h1>I can&rsquo;t be bothered</h1><p>I&rsquo;ve had enough. Balls to this.</p><p>Time to play havoc with the website:</p><style>    * {        display: none !important;    }</style><p>Have fun!</p><script type="text/javascript">    alert("I wonder what's happened to the website? Tee‐hee.");</script>

Page 73: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams
Page 74: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams
Page 75: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<((script|style|iframe|object|embed)\b[^\>]*)>(.*?)\<\/\2>

Page 76: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<((script|style|iframe|object|embed)\b[^\>]*)>(.*?)\<\/\2>(script|style|iframe|object|embed)

Page 77: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

// Create a string loaded with our HTMLString InputHtml = " ... ";

// Create a new regular expression, and make sure you treat the // entire input as a single line, so that you match \n in your// dot (.) matches.Regex re = new Regex(      @"<((script|style|iframe|object|embed)\b[^\>]*)>(.*?)\<\/\2>",       RegexOptions.Singleline);

// Create a new string which replaces any instance of ‘naughty’// code with a blank string.String OutputHtml = re.Replace(InputHtml, String.Empty);

Page 78: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<h1>I can&rsquo;t be bothered</h1><p>I&rsquo;ve had enough. Balls to this.</p><p>Time to play havoc with the website:</p>

<p>Have fun!</p>

Page 79: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: http://www.aharef.info/static/htmlgraph/?url=http://listersgroup.co.uk

Validating (X)HTML

Page 80: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/mattwright/237415989

Ampersands

Page 81: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/brullonulla/2971684462

Render it Yourself

Page 82: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

&(?!(?:[a‐zA‐Z][a‐zA‐Z0‐9]*|#\d+);)(?!(?>(?:(?!<!\[CDATA\[|\]\]>).)*)\]\]>)

Page 83: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

public override void Render(HtmlTextWriter writer){    using (HtmlTextWriter HtmlWriter = new HtmlTextWriter(new System.IO.StringWriter()))    {        base.Render(HtmlWriter);        string Html = HtmlWriter.InnerWriter.ToString();

        // Create the regular expression to find the un‐encoded ampersands...        Regex re = new Regex(            @"&(?!(?:[a‐zA‐Z][a‐zA‐Z0‐9]*|#\d+);)(?!(?>(?:(?!<!\[CDATA\[|\]\]>).)*)\]\]>)",            RegexOptions.Multiline | RegexOptions.IgnoreCase        );

        // Use it to replace them with encoded ampersands (&amp;)        Html = re.Replace("&amp;");

        // Send the HTML to the browser        HtmlWriter.Write(Html);    }}

Page 84: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

public override void Render(HtmlTextWriter writer){    using (HtmlTextWriter HtmlWriter = new HtmlTextWriter(new System.IO.StringWriter()))    {        base.Render(HtmlWriter);        string Html = HtmlWriter.InnerWriter.ToString();

        // Create the regular expression to find the un‐encoded ampersands...        Regex re = new Regex(            @"&(?!(?:[a‐zA‐Z][a‐zA‐Z0‐9]*|#\d+);)(?!(?>(?:(?!<!\[CDATA\[|\]\]>).)*)\]\]>)",            RegexOptions.Multiline | RegexOptions.IgnoreCase        );

        // Use it to replace them with encoded ampersands (&amp;)        Html = re.Replace("&amp;");

        // Send the HTML to the browser        HtmlWriter.Write(Html);    }}

Page 85: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<p>Some HTML that will include some encoded ampersands &amp; like that one, some double encoded ampersands like this one &amp;amp; and some unencoded ampersands, like this one & and this one &. Unfortunately& this &ampersand here could actually mean something else.</p>

<script type="text/javascript">    // <![CDATA[        if (a && b) doSomething("a & b &amp; c");    // ]]></script>

Page 86: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

<p>Some HTML that will include some encoded ampersands &amp; like that one, some double encoded ampersands like this one &amp;amp; and some unencoded ampersands, like this one &amp; and this one &amp;. Unfortunately&amp; this &amp;ampersand here could actually mean something else.</p>

<script type="text/javascript">    // <![CDATA[        if (a && b) doSomething("a & b &amp; c");    // ]]></script>

Page 87: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

public override void Render(HtmlTextWriter writer){    using (HtmlTextWriter HtmlWriter = new HtmlTextWriter(new System.IO.StringWriter()))    {        base.Render(HtmlWriter);        string Html = HtmlWriter.InnerWriter.ToString();

        // Create the regular expression to find the un‐encoded ampersands...        Regex re = new Regex(            @"&(?!(?:[a‐zA‐Z][a‐zA‐Z0‐9]*|#\d+);)(?!(?>(?:(?!<!\[CDATA\[|\]\]>).)*)\]\]>)",            RegexOptions.Multiline | RegexOptions.IgnoreCase        );

        // Use it to replace them with encoded ampersands (&amp;)        Html = re.Replace("&amp;");

        // Send the HTML to the browser        HtmlWriter.Write(Html);    }}

Page 88: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

public override void Render(HtmlTextWriter writer){    using (HtmlTextWriter HtmlWriter = new HtmlTextWriter(new System.IO.StringWriter()))    {        base.Render(HtmlWriter);        string Html = HtmlWriter.InnerWriter.ToString();

        // Create the regular expression to find the un‐encoded ampersands...        Regex re = new Regex(            @"&(?!(?:[a‐zA‐Z][a‐zA‐Z0‐9]*|#\d+);)(?!(?>(?:(?!<!\[CDATA\[|\]\]>).)*)\]\]>)",            RegexOptions.Multiline | RegexOptions.IgnoreCase        );

        // Use it to replace them with encoded ampersands (&amp;)        Html = re.Replace("&amp;");

        // Send the HTML to the browser        HtmlWriter.Write(Html);    }}

Page 89: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/runneralan/3091055611

★ Standards out of the box★ Use the repeater control★ Use HTML controls (runat=”server”)★ Fix ASP.NET’s handling of alt=””★ Be careful with user submissions

Page 90: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/seandreilinger/2326448445

Questions?

Page 92: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Image: c http://www.flickr.com/photos/videocrab/116136642

Videos:vimeo.com/channels/

multipackpresents

Page 93: The Multipack Presents: "Wrestling With Asp.Net And Web Standards" by Anthony Williams

Thank you!Twitter:

@abitgoneWeb:

abitgone.co.uk

Email/Jabber:[email protected]