1.前段代码
<asp:DataList ID="DataList1" runat="server" >
<ItemTemplate>
<li class=Sub>
<a href="Leave.aspx?Name=<%#Eval("Uid")%>"><span><%#Eval("Uid") %></span></a>
<ul class=list-unstyled>
<li>
<a href="ShowWords.aspx?subjectID=<%#Eval("ID")%>">
<%#Eval("Subject")%><!--绑定数据-->
</a>
</li>
<li class=DateTime>
<%#Eval("leaveTime") %>
</li>
</ul>
</li>
</ItemTemplate>
<FooterTemplate >
<br />
<div id="footer">
<asp:Label ID="labPageCount" runat="server" style="font-weight:bold;" Text="Label"></asp:Label>/
<asp:Label ID="labCurrentPage" runat="server" Text="Label"></asp:Label>
<asp:Button ID="lnkbtnFirst" class="btn btn-default" runat="server" CommandName="first" Text="首页"></asp:Button>
<asp:Button ID="lnkbtnFront" class="btn btn-default" runat="server" CommandName="pre" Text="<< 上一页"></asp:Button>
<asp:Button ID="lnkbtnNext" class="btn btn-default" runat="server" CommandName="next" Text="下一页 >>"></asp:Button>
<asp:Button ID="lnkbtnLast" class="btn btn-default" runat="server" CommandName="last" Text="尾页"></asp:Button>
<span>跳转至:</span>
<asp:TextBox ID="txtPage" class="form-control" runat="server" style="width:40px;display:inline;"></asp:TextBox>
<asp:LinkButton ID="lnkbtnSearch" class="btn btn-success" runat="server" CommandName="search">Go</asp:LinkButton>
</div>
<div style="clear:both"> </div>
</FooterTemplate>
</asp:DataList>
2.后端代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList(0);
}
private void BindDataList(int currentpage)
{
pds.AllowPaging = true;//允许分页
pds.PageSize = 10;//每页显示的信息条数
pds.CurrentPageIndex = currentpage;
string strSql = "select *from tb_leaveword order by leaveTime desc";//mysql查询语句
conn.Open();//打开数据库
MySqlDataAdapter sda = new MySqlDataAdapter(strSql, conn);//执行mysql语句
DataSet ds = new DataSet();
sda.Fill(ds);//填充数据集
pds.DataSource = ds.Tables[0].DefaultView;
DataList1.DataSource = pds;//设置DataList的数据源
DataList1.DataBind();//绑定数据
conn.Close();//关闭数据库
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;//获取DataList控件中的其他控件对象
Label PageCount = e.Item.FindControl("labPageCount") as Label;
Button FirstPage = e.Item.FindControl("lnkbtnFirst") as Button;
Button PrePage = e.Item.FindControl("lnkbtnFront") as Button;
Button NextPage = e.Item.FindControl("lnkbtnNext") as Button;
Button LastPage = e.Item.FindControl("lnkbtnLast") as Button;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
PageCount.Text = pds.PageCount.ToString();
if (pds.IsFirstPage)//如果当前页是首页
{
FirstPage.Enabled = false;//首页按钮禁止
PrePage.Enabled = false;//上一页按钮禁止
}
if (pds.IsLastPage)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "first":
pds.CurrentPageIndex = 0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre":
pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next":
pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last":
pds.CurrentPageIndex = pds.PageCount - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "search":
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(pds.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
{
MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
}
if (MyPageNum <= 0 || MyPageNum > PageCount)
{
Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
}
else
{
BindDataList(MyPageNum - 1);
}
}
break;
}
}
}