博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以前对于数据绑定列表控件一直使用的是gridview,因为方便快捷嘛。后来高人说repeater控件比较好,生成的客户端代码利于SEO,并且效率也高。所以就尝试着将gridview能办到的事情用repeater控件实现一下。

先来个效果图

------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------

主要有一下几个知识点:

1.显示序号列<%# Container.ItemIndex +1 %>

这个是在不分页的情况下,如果分页的话第N页都是从1~pagesize显示的,不会接着上一页的序号显示。所以要在后台另构造序号列column来解决

2.获取选中行的主键

repeater控件没有DataKeyNames属性,所以这里要放一个Label或者HiddenField来绑定主键ID

3.分页

repeater控件没有自己的分页属性,所以要完全自己来定义分页代码。这里每次只装在一页数据。

4.没有数据的时候显示表头  Visible="<%#Repeater1.Items.Count==0 %>"

 

前端代码:

View Code
<body>
<form id="form1" runat="server">
<div id="rplist">
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True"
oncheckedchanged
="chkAll_CheckedChanged" Text="全选" />
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table>
<tr><td colspan="6" style="text-align:center; background-color:#76a0ae; color:#ffffff; height:20px">新闻列表</td></tr>
<tr style="color:#1e486e">
<td>序号</td>
<td>新闻标题</td>
<td>发布时间</td>
<td colspan="3">操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:HiddenField ID="hfId" Value='<%#Eval("ID")%>' runat="server" />
<%# Eval("order")%></td>
<td class="tdleft"><%#Eval("NewsTitle")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"CreateTime","{0:yyyy-M-d}")%></td>
<td><img alt="编辑" src="News/Images/writting.png" height="14" width="14"/>
<asp:LinkButton ID="lnkEdit" CommandName="EditData" runat="server">Edit</asp:LinkButton></td>
<td><img alt="删除" height="13" width="13" src="News/Images/cross.png"/>
<asp:LinkButton ID="lnkDelete" CommandName="DelData" runat="server">Delete</asp:LinkButton></td>
<td><asp:CheckBox ID="chkNews" runat="server" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style=" background-color:#f0f5f8">
<td><asp:HiddenField ID="hfId" Value='<%#Eval("ID")%>' runat="server" />
<%# Eval("order")%></td>
<td class="tdleft"><%#Eval("NewsTitle")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"CreateTime","{0:yyyy-M-d}")%></td>
<td><img alt="编辑" src="News/Images/writting.png" height="14" width="14"/>
<asp:LinkButton ID="lnkEdit" CommandName="EditData" runat="server">Edit</asp:LinkButton></td>
<td><img alt="删除" height="13" width="13" src="News/Images/cross.png"/>
<asp:LinkButton ID="lnkDelete" CommandName="DelData" runat="server">Delete</asp:LinkButton></td>
<td><asp:CheckBox ID="chkNews" runat="server" /></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr id="noData" runat="server" Visible="<%#Repeater1.Items.Count==0 %>"  >
<td colspan="6">没有相关的数据</td>
   </tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<div id="pager">
<table>
<tr>
<td>总记录数:<asp:Label ID="lblCount" runat="server" ></asp:Label></td>
<td ><asp:Label ID="lblCurrentPage" runat="server"></asp:Label>/
<asp:Label ID="labPage" runat="server"></asp:Label></td>
<td>
<asp:LinkButton ID="first" runat="server" onclick="first_Click">首页</asp:LinkButton>
<asp:LinkButton ID="up" runat="server" onclick="up_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="next" runat="server" onclick="next_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="last" runat="server" onclick="last_Click">末页</asp:LinkButton>
</td>
<td>跳转:<asp:TextBox ID="txtPage" Width="50px" runat="server"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" onclick="btnGo_Click" />
</td>
</tr>
</table>
</div>


</form>
</body>

后台代码:

页面加载pageload

View Code
            lblCurrentPage.Text = "1";
BindData();

分页及数据绑定

View Code
        private void BindData()
{
string filter = NewsFilter();
CNewsList aNewsList
= new CNewsList();

int Totalcount = aNewsList.NewsCount(filter);//获取所有数据的数量
int pagesize = 8;
int curpage = Convert.ToInt32(lblCurrentPage.Text);//当前第几页

DataTable dt
= aNewsList.GetNewsList(curpage, pagesize, filter);//每次只获取第N页pagesize条数据
DataColumn dcOrder = new DataColumn("order", typeof(int));//自定义一个列用来显示序号
dt.Columns.Add(dcOrder);
for (int i = 0; i < dt.Rows.Count; i++)
{
//(itemIndex+1)*(pageIndex-1)*pagesize
dt.Rows[i]["order"] = i + 1 + (curpage - 1) * 8;
}


//PagedDataSource pds = new PagedDataSource();//分页
//pds.DataSource = dt.DefaultView;
//pds.AllowPaging = true;//AllowPaging和AllowCustomPaging都要写上=true
//pds.AllowCustomPaging = true;//虚拟的页数,主要为了显示分页(不用装载所有数据)
//pds.VirtualCount = Totalcount;
//pds.PageSize = pagesize;
//pds.CurrentPageIndex = curpage - 1;
//int pages = pds.PageCount;
//上面注释掉的7行代码等于下面的一句代码(这里pagedatasource用的有点傻,呵呵)

int pages = pageCount(pagesize, Totalcount);

lblCount.Text
= Totalcount.ToString();//总记录数
next.Enabled = true;//下一页
up.Enabled = true;//上一页
first.Enabled = true;//首页
last.Enabled = true;//末页
if (curpage == 1)
{
up.Enabled
= false;
first.Enabled
= false;
}
if (curpage == pages)
{
next.Enabled
= false;
last.Enabled
= false;
}
labPage.Text
= pages.ToString();//总页数

Repeater1.DataSource
= dt;
Repeater1.DataBind();
}
View Code
        protected void first_Click(object sender, EventArgs e)
{
lblCurrentPage.Text
= "1";
BindData();

}
View Code
        protected void next_Click(object sender, EventArgs e)
{
int page = int.Parse(lblCurrentPage.Text.Trim());
lblCurrentPage.Text
= (page + 1).ToString();
BindData();
}
View Code
        protected void up_Click(object sender, EventArgs e)
{
int page = int.Parse(lblCurrentPage.Text.Trim());
lblCurrentPage.Text
= (page - 1).ToString();
BindData();
}
View Code
        protected void last_Click(object sender, EventArgs e)
{
int page = int.Parse(labPage.Text.Trim());
lblCurrentPage.Text
= page.ToString();
BindData();
}
View Code
        protected void btnGo_Click(object sender, EventArgs e)
{
//转到第N页代码
int curPage = int.Parse(txtPage.Text.Trim());
int allPage = int.Parse(labPage.Text.Trim());
if (curPage > allPage)
curPage
= allPage;
txtPage.Text
=lblCurrentPage.Text = curPage.ToString();
BindData();
}

获取主键

View Code
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int row = e.Item.ItemIndex;
HiddenField hfId
= e.Item.FindControl("hfId") as HiddenField;
int Id = int.Parse(hfId.Value);

string cmdName = e.CommandName.Trim();
if (cmdName.Equals("EditData"))
{

}
else if(cmdName.Equals("DelData"))
{

}
else
return;
}
}

根据选中的页Index和每页显示多少数据,来查询数据表

View Code
        public DataTable GetNewsList(int PageIndex, int PageSize)
{
string strSql = "select top (" + PageSize + ") id,newstitle,createtime from " + m_nameOfMainTable
+ " where id not in (select top(" + PageSize + "*(" + PageIndex + "-1)) Id from " + m_nameOfMainTable + ")";
DataSet ds
= new DataSet();
m_db.ExecSeleSql(
ref ds, strSql);
return ds.Tables[0];

}

计算当前共有多少页的代码

View Code
        private int pageCount(int pageSize,int recordCount)
{
if (recordCount == 0)
return 1;
int count = 0;
count
= recordCount / pageSize;
if (recordCount % pageSize != 0)
count
+= 1;
return count;
}