Repater 控件的传统应用
<asp:Repeater ID="normalArticleList" runat="server">
<ItemTemplate>
<div class="textbox">
<div class="textbox-title">
<h4><a onclick="<%# urlFormats.ArticleConent(Convert.ToInt32(Eval("ArticleID").ToString()))%>" href="#"><span runat=server visible=<%# Eval("IsTop")%>>[置顶]</span><%# Eval("Title")%></a>
</h4>
<div class="textbox-label">
[
<%# Eval("PostTime")%>
| <%=lang["author"]%>: <a onclick="<%# urlFormats.UserDetail(int.Parse(Eval("AuthorID").ToString()))%>" href="#"><%# Eval("Author") %></a> ]</div>
</div>
<div class="textbox-content">
<%# GetContent(Eval("Content0").ToString(), Eval("Content1").ToString())%><%--<%# Eval("Content1") %>--%></div>
<div class="textbox-bottom">
<asp:PlaceHolder ID="actionLinkPanel" runat="server">
<a href="articleManage.aspx?act=edit&id=<%# Eval("ArticleID") %>" title="<%=lang["edit"]%>">
<img src="/styles/default/images/icon_edit.gif" alt="<%=lang["edit"]%>" />
</a>| <a href="/articleManage.aspx?act=delete&id=<%# Eval("ArticleID") %>" title="<%=lang["delete"]%>">
<img src="/styles/default/images/icon_del.gif" alt="<%=lang["delete"]%>" /></a> |
</asp:PlaceHolder>
<a onclick="<%# urlFormats.ArticleCategoryUrl(Convert.ToInt32(Eval("categoryID").ToString()))%>" href="#"><%=lang["category"]%>:<%# Eval("CategoryName")%></a> | <a href="?id=<%# Eval("ArticleID") %>"><%=lang["permalink"]%></a> | <a onclick="<%# urlFormats.ArticleConent(Convert.ToInt32(Eval("ArticleID").ToString()))%>" href="#commentbox" title="评论"><%=lang["comments"]%>:<%# Eval("CommentCount") %></a> | <a href="#"><%=lang["trackbacks"]%>:<%# Eval("TrackbackCount") %></a> | <%=lang["views"]%>:<%# Eval("ViewCount") %>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<ItemTemplate>
<div class="textbox">
<div class="textbox-title">
<h4><a onclick="<%# urlFormats.ArticleConent(Convert.ToInt32(Eval("ArticleID").ToString()))%>" href="#"><span runat=server visible=<%# Eval("IsTop")%>>[置顶]</span><%# Eval("Title")%></a>
</h4>
<div class="textbox-label">
[
<%# Eval("PostTime")%>
| <%=lang["author"]%>: <a onclick="<%# urlFormats.UserDetail(int.Parse(Eval("AuthorID").ToString()))%>" href="#"><%# Eval("Author") %></a> ]</div>
</div>
<div class="textbox-content">
<%# GetContent(Eval("Content0").ToString(), Eval("Content1").ToString())%><%--<%# Eval("Content1") %>--%></div>
<div class="textbox-bottom">
<asp:PlaceHolder ID="actionLinkPanel" runat="server">
<a href="articleManage.aspx?act=edit&id=<%# Eval("ArticleID") %>" title="<%=lang["edit"]%>">
<img src="/styles/default/images/icon_edit.gif" alt="<%=lang["edit"]%>" />
</a>| <a href="/articleManage.aspx?act=delete&id=<%# Eval("ArticleID") %>" title="<%=lang["delete"]%>">
<img src="/styles/default/images/icon_del.gif" alt="<%=lang["delete"]%>" /></a> |
</asp:PlaceHolder>
<a onclick="<%# urlFormats.ArticleCategoryUrl(Convert.ToInt32(Eval("categoryID").ToString()))%>" href="#"><%=lang["category"]%>:<%# Eval("CategoryName")%></a> | <a href="?id=<%# Eval("ArticleID") %>"><%=lang["permalink"]%></a> | <a onclick="<%# urlFormats.ArticleConent(Convert.ToInt32(Eval("ArticleID").ToString()))%>" href="#commentbox" title="评论"><%=lang["comments"]%>:<%# Eval("CommentCount") %></a> | <a href="#"><%=lang["trackbacks"]%>:<%# Eval("TrackbackCount") %></a> | <%=lang["views"]%>:<%# Eval("ViewCount") %>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
上面的代码通过数据绑定语法,能很好的完成了工作。可这界面太烦,字段多,金黄色语法就更多,看上去很烦,但我们却无法逃避这个问题,而且有时候还需要对一些字段进行逻辑判断。
Repater 控件的友好应用
用 Literal 控件代替绑定语法,在 ItemDataBound 事件中完成数据的填充。
前端代码:
<asp:Repeater ID="normalArticleList" runat="server">
<ItemTemplate>
<div class="textbox">
<div class="textbox-title">
<asp:Literal ID="articleTitle" runat="server"></asp:Literal>
<div class="textbox-label">
<asp:Literal ID="articleLabel" runat="server"></asp:Literal>
</div>
</div>
<div class="textbox-content">
<asp:Literal ID="articleContent" runat="server"></asp:Literal>
</div>
<div class="textbox-bottom">
<asp:Literal ID="articleBottom" runat="server"></asp:Literal>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<ItemTemplate>
<div class="textbox">
<div class="textbox-title">
<asp:Literal ID="articleTitle" runat="server"></asp:Literal>
<div class="textbox-label">
<asp:Literal ID="articleLabel" runat="server"></asp:Literal>
</div>
</div>
<div class="textbox-content">
<asp:Literal ID="articleContent" runat="server"></asp:Literal>
</div>
<div class="textbox-bottom">
<asp:Literal ID="articleBottom" runat="server"></asp:Literal>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
后端代码:
void normalArticleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Article item = (Article)e.Item.DataItem;
Literal title = (Literal)e.Item.FindControl("articleTitle");
Literal articleLabel = (Literal)e.Item.FindControl("articleLabel");
Literal articleContent = (Literal)e.Item.FindControl("articleContent");
Literal articleBottom = (Literal)e.Item.FindControl("articleBottom");
title.Text = RenderNormalTitle(item);
articleLabel.Text = RenderNormalLabel(item);
articleContent.Text = RenderNormalContent(item);
articleBottom.Text = RenderNormalBottom(item);
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Article item = (Article)e.Item.DataItem;
Literal title = (Literal)e.Item.FindControl("articleTitle");
Literal articleLabel = (Literal)e.Item.FindControl("articleLabel");
Literal articleContent = (Literal)e.Item.FindControl("articleContent");
Literal articleBottom = (Literal)e.Item.FindControl("articleBottom");
title.Text = RenderNormalTitle(item);
articleLabel.Text = RenderNormalLabel(item);
articleContent.Text = RenderNormalContent(item);
articleBottom.Text = RenderNormalBottom(item);
}
}
这样的写法会漂亮很多,而且数据绑定是通过反射机制的,但是听说会损失性能!
学习来源:http://www.cnblogs.com/Clingingboy/archive/2007/12/31/1021611.html
文章来源:http://www.cnblogs.com/xugang/archive/2008/01/01/1022368.html