Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wrestling with ASP.NET and Web Standards

Wrestling with ASP.NET and Web Standards

Despite some of the things you may hear, Microsoft's web platform is actually very good at serving standards-based, usable websites – it's just that, sometimes, it needs a bit of a helping hand. This presentation, given at the Multipack's Coding Standards Event in June 2009, gave some practical tips on how to do just that.

Anthony Williams

September 27, 2011
Tweet

More Decks by Anthony Williams

Other Decks in Programming

Transcript

  1. <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>
  2. <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>
  3. //$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$M$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",$"");
  4. 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