datalist分页实例
html代码:
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td style="width:20%">
IP</td>
<td style="width:20%">
登录时间</td>
<td style="width:20%">
离开时间</td>
<td style="width:20%">
浏览器</td>
<td style="width:20%">
操作系统</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td style="width:20%">
<%# DataBinder.Eval(Container.DataItem,"IP") %></td>
<td style="width:20%">
<%# DataBinder.Eval(Container.DataItem,"LoginTime") %></td>
<td style="width:20%">
<%# DataBinder.Eval(Container.DataItem,"LeaveTime") %></td>
<td style="width:20%">
<%# DataBinder.Eval(Container.DataItem,"Browser") %></td>
<td style="width:20%">
<%# DataBinder.Eval(Container.DataItem,"OS") %></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
共有<asp:Label ID="labcount" runat="server" Text=""></asp:Label>
页 当前
<asp:Label ID="labnowpage" runat="server" Text="1"></asp:Label>
页
<asp:LinkButton ID="lkbtnfirst" runat="server" onclick="lkbtnfirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lkbtnfront" runat="server" onclick="lkbtnfront_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lkbtnnext" runat="server" onclick="lkbtnnext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lkbtnlast" runat="server" onclick="lkbtnlast_Click">尾页</asp:LinkButton>
</form>
datalist分页是通过PagedDataSource类来实现的。该类封装数据绑定控件与分页相关的属性,以允许该控件执行分页操作。
后台代码
using System.data.sqlclient;
DB db = new DB();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
int currentpage = Convert.ToInt32(labnowpage.Text);
PagedDataSource ps = new PagedDataSource();//生成pageddatasource的实例
string selstr = "select * from tb_CounterInfo";
string slqcon = ConfigurationManager.AppSettings["getcon"].ToString();
SqlConnection con = new SqlConnection(slqcon);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(selstr,con);
DataSet ds=new DataSet();
da.Fill(ds, "CounterInfo");
ps.DataSource = ds.Tables["CounterInfo"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 4;
ps.CurrentPageIndex = currentpage - 1;
lkbtnfirst.Enabled = true;
lkbtnfront.Enabled = true;
lkbtnnext.Enabled = true;
lkbtnlast.Enabled = true;
if (currentpage == 1)
{
lkbtnlast.Enabled = false;
lkbtnfront.Enabled = false;
}
if (currentpage == ps.PageCount)
{
lkbtnnext.Enabled = false;
lkbtnlast.Enabled = false;
}
this.labcount.Text = Convert.ToString(ps.PageCount);
this.DataList1.DataSource = ps;
this.DataList1.DataKeyField = "ID";
this.DataList1.DataBind();
}
protected void lkbtnfirst_Click(object sender, EventArgs e)
{
this.labnowpage.Text = "1";
bind();
}
protected void lkbtnfront_Click(object sender, EventArgs e)
{
this.labnowpage.Text = Convert.ToString(Convert.ToInt32(this.labnowpage.Text) - 1);
bind();
}
protected void lkbtnnext_Click(object sender, EventArgs e)
{
this.labnowpage.Text = Convert.ToString(Convert.ToInt32(this.labnowpage.Text) + 1);
bind();
}
protected void lkbtnlast_Click(object sender, EventArgs e)
{
this.labnowpage.Text = this.labcount.Text;
bind();
}
获取DataList控件中控件数据的方法通常使用两种方法:
一种是通过e.Item.Controls[0]索引直接访问Item中的控件,另一种方法是使用FindControl()方法来查找。