前台代码:
<asp:ListView ID="ListView1" runat="server" GroupItemCount="4"
onpagepropertieschanged="ListView1_PagePropertiesChanged" >
<!-- LayoutTemplate控件中放占位符控件 -->
<LayoutTemplate>
<table runat="server" id="groupPlaceholderContainer">
<tr runat="server" id="groupPlaceholder"></tr>
</table>
<!-- 分页控件 -->
<asp:DataPager ID="dpNewBook" runat="server" PagedControlID="ListView1" PageSize="12">
<Fields>
<asp:NumericPagerField ButtonCount="10" NextPageText="下一页" PreviousPageText="上一页" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<!-- ItemTemplate控件中放内容控件,也就是循环显示的内容 -->
<ItemTemplate>
<td runat="server">
<a href="#">
<img src='Images/BookCovers/<%#Eval("ISBN") %>.jpg' alt='<%#Eval("Title") %>' width="100px"
height="120px" /></a><br />
<a href="#">
<%#CutString2(Eval("Title"),15)%></a><br />
<%#Eval("UnitPrice","{0:C2}")%></a>
</td>
</ItemTemplate>
<!-- 如果ListView控件中设置了GroupItemCount属性就必须使用GroupTemplate控件 -->
<GroupTemplate>
<tr runat="server" id="itemPlaceholderContainer">
<td runat="server" id="itemPlaceholder"></td>
</tr>
</GroupTemplate>
</asp:ListView>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindListView();//将List中的数据绑定到ListView中
}
}
/// <summary>
/// 将List中的数据绑定到ListView中
/// </summary>
private void bindListView()
{
this.ListView1.DataSource = BooksManager.GetBooks();
this.ListView1.DataBind();
}
#region 截断图书显示内容
/// <summary>
/// 截断图书显示内容
/// </summary>
public static string CutString2(object content, int length)
{
string temp = content.ToString().Replace("<br/>", "").Replace("<br>", "");//先替换换行标签,保证不出现换行
//参数说明:要处理的字符串,符合条件的表达式[汉字],替换的字符[内容随意写但是要两个字符,因为一个中文对应两个字符,不区分大小写]
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= length)
{
return temp;
}
for (int i = temp.Length; i >= 0; i--)
{
temp = temp.Substring(0, i);
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= length - 3)
{
return temp + "...";
}
}
return "";
}
#endregion
/// <summary>
/// 单击页码时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
{
//将List中的数据绑定到ListView中
bindListView();
}