DataList是不具备自动分页的功能的......
DataList与DataGrid都是很好用的控件,但是非常遗憾的是DataList不具有象DataGrid那样的自动分页功能,即:他不具备PageSize这样的属性来控制分页,而DataGrid却不具备DataList的RepeatDirection和 RepeatColumns属性。发布这个,希望能够帮到刚刚学习.net的朋友
a.aspx页:
<TABLE cellSpacing=0 cellPadding=0 width=784 height="120" align=center border=0>
<tr>
<td align=center>
<asp:datalist id="MyDataGrid" RepeatDirection=Horizontal RepeatColumns=5 showfooter=true borderwidth=0 runat=server>
<HeaderTemplate>
<table width=784 cellpadding=0 cellspacing=0 border=0 >
<tr>
</HeaderTemplate>
<ItemTemplate>
<td align=center>
<table width="100%" height="120">
<tr>
<td width=154 align="center">
<img width=130 height=90 src='<%# DataBinder.Eval(Container.DataItem, "P_ID", "Img.aspx?id={0}")%>'>
</td>
</tr>
<tr>
<td align=center>
名称: <%# DataBinder.Eval(Container.DataItem, "P_Name")%>
</td>
</tr>
</table>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:datalist>
</td>
</tr>
</table>
<table id=ttt width=784 align=center>
<tr align="center" valign="middle" width=100%>
<td align="center" valign="middle"><asp:LinkButton ID="btnFrist" runat="server" CommandName="Pager" CommandArgument="Frist" OnCommand="PagerButtonClick" Text="首页"></asp:LinkButton></td>
<td align="center" valign="middle"><asp:LinkButton ID="btnPrev" runat="server" CommandName="Pager" CommandArgument="Prev" OnCommand="PagerButtonClick" Text="上一页"></asp:LinkButton></td>
<td align="center" valign="middle"><asp:LinkButton ID="btnNext" runat="server" CommandName="Pager" CommandArgument="Next" OnCommand="PagerButtonClick" Text="下一页"></asp:LinkButton></td>
<td align="center" valign="middle"><asp:LinkButton ID="btnLast" runat="server" CommandName="Pager" CommandArgument="Last" OnCommand="PagerButtonClick" Text="尾页"></asp:LinkButton></td>
<td align="right"valign="middle">页<asp:Label ID="lblCurrentPage" runat="server" /></td><td align="left" valign="middle">/<asp:Label ID="lalPageCount" runat="server"/></td>
<td align="center" valign="middle">共<asp:Label ID="lblRecordCount" runat="server" />条记录</td>
<td align="center" valign="middle">转到第<asp:DropDownList ID="D1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="D1_SelectedIndexChanged" />页</td>
</tr>
</td>
</tr>
</table>
a.aspx.cs页:
private static int PageIndex;//保存当前页的索引
private static int pagesize;//保存每个页面中的记录数目
private static int CurrentPage;//为当前页序号-1
private static int PageCount;//保存总页数
private static int RecordCount;//总记录数
public bool DropFill;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropFill = true;
pagesize = 10;//pagesize = MyDataGrid.RepeatColumns;可以设DataList的属性RepeatDirection="Horizontal" RepeatColumns="10",通过RepeatColumns来设置每页显示的条数
CurrentPage = 0;
GetPageCount();//得到总记录数
Databind();
}
}
public void GetPageCount()
{
string sql = "select * from prizes ";
Conn c1 = new Conn();
SqlCommand cmd = new SqlCommand(sql, c1.SqlConnection());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "table");
RecordCount = ds.Tables["table"].DefaultView.Count;
c1.SqlConnection().Close();
}
public void Databind()
{ D1.Items.Clear();
string sql = "select * from prizes";
Class1 c1 = new Class1();
SqlCommand cmd = new SqlCommand(sql, c1.SqlConnection());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
PageIndex = CurrentPage * pagesize;
da.Fill(ds,PageIndex,pagesize, "table");
MyDataGrid.DataSource = ds.Tables["table"];
MyDataGrid.DataBind();
c1.CloseSqlConnection();
PageCount = RecordCount / pagesize;
if (RecordCount % pagesize != 0)
PageCount++;
lalPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
if (lblCurrentPage.Text == "")
{
lblCurrentPage.Text = "1";
}
if (CurrentPage == 0)
{
btnFrist.Enabled = false;
btnPrev.Enabled = false;
}
if (CurrentPage == PageCount - 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
//给下拉链表中添加页数
if (DropFill == true)
{
for (int i = 1; i <= PageCount; i++)
{
D1.Items.Add(new ListItem(i.ToString()));
}
}
else
{
D1.Items.Add("1");
}
}
protected void D1_SelectedIndexChanged(object sender, EventArgs e)
{
btnFrist.Enabled = true;
btnLast.Enabled = true;
btnNext.Enabled = true;
btnPrev.Enabled = true;
DropFill = false;
CurrentPage = Int32.Parse(D1.SelectedValue.ToString())-1;
//MyDataGrid.CurrentPageIndex = Pageindex - 1;
DropFill = false;
Databind();
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
protected void PagerButtonClick(object sender, CommandEventArgs e)
{
btnFrist.Enabled = true;
btnLast.Enabled = true;
btnNext.Enabled = true;
btnPrev.Enabled = true;
DropFill = false;
string age = e.CommandArgument.ToString();
switch (age)
{
case "Next":
if (CurrentPage < PageCount - 1)
{
CurrentPage++;
}
break;
case "Prev":
if (CurrentPage > 0)
{
CurrentPage--;
}
break;
case "Frist":
{
CurrentPage = 0;
}
break;
case "Last":
{
CurrentPage = PageCount - 1;
}
break;
}
//MyDataGrid.CurrentPageIndex = PageIndex;
Databind();
lblCurrentPage.Text = Convert.ToString(CurrentPage+1);
}