How to optimize repeater white-spaces
In my repeater I have the following markup:
<asp:Repeater runat="server" id="TeamsRepeater" OnItemDataBound="TeamsRepeater_ItemDataBound" ClientIDMode="Predictable">
<ItemTemplate>
<tr runat="server" id="team">
<td><%# Container.ItemIndex + 1 %></td>
<td><%#Eval("PosChange")%></td>
<th><%# ((ITeam)Eval("MemberTeam")).Href()%></th>
<td><%#Eval("GamesAll")%></td>
<td><%#Eval("GameW")%></td>
<td><%#Eval("GameD")%></td>
<td><%#Eval("GameL")%></td>
<td><%#((ITournMember)Container.DataItem).Goals()%></td>
<td><%#Eval("Score")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
That is pretty readable (and maintainable, please point me if you see something can be done better).
But my concern is that HTML code generated for this markup contains a huge amount of spaces...
How could I enhance that in order:
- remove unnecessary space开发者_开发技巧s from output html;
- in the same time: keep markup readability?
You could try this HttpModule that removes whitespace.
I wouldn't sacrifice readability to get rid of spaces. This will hurt you in the long run. Instead enable compression for your dynamic content, i.e. with IIS HTTP Compression - the end result will be dynamic gzip compression on your aspx pages that are much smaller than your plain HTML.
Creating a module works great but if you are using Update Panels and AJAX that would not work since the AJAX request gets processed by the size of the response. When the response is changed the AJAX javascript fails. Therefore I created a Repeater control that trims its response before sending it back:
<Assembly: TagPrefix("MyControls", "MyRepeater")>
''' <summary>
''' Summary description for MyRepeater.
''' </summary>
<ToolboxData("<{0}:MyRepeater runat=server></{0}:MyRepeater>")>
Public Class MyRepeater : Inherits Repeater
Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter)
Using htmlwriter As New HtmlTextWriter(New System.IO.StringWriter())
MyBase.Render(htmlwriter)
Dim html As String = htmlwriter.InnerWriter.ToString()
html = Regex.Replace(html, "(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}", String.Empty)
html = Regex.Replace(html, "[ \f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&|^!~?:=])[\f\r\t\v]?", "$1")
html = html.Replace(";\n", ";")
writer.Write(html.Trim())
End Using
End Sub
End Class
Then on your aspx page enter this to register the control
<%@ Register TagPrefix="ccl" Assembly="MyControls" Namespace="MyControls" %>
@Budda, may be my decision is stupid, but it works and probably readable :)
<table>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="CustomersSource">
<ItemTemplate><%# string.Format("<tr>{0}</tr>",
string.Format("<td>{0}</td>",
string.Join("</td><td>",
new[]
{
Eval("CustomerID"),
Eval("CompanyName")
}))) %></ItemTemplate>
</asp:Repeater>
</table>
If move code to helper method we'll get more elegant decision:
public static class HtmlHelper
{
public static string TableRow(params string[] tdList)
{
return string.Format("<tr>{0}</tr>", string.Format("<td>{0}</td>", string.Join("</td><td>", tdList)));
}
}
<table>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="CustomersSource">
<ItemTemplate><%# HtmlHelper.TableRow(
Eval("CustomerID").ToString(),
Eval("CompanyName").ToString()) %></ItemTemplate>
</asp:Repeater>
</table>
精彩评论