.NET 分页读取ACCESS数据库数据
前台:
<div class="table">
<asp:Repeater ID="rptPLHList" runat="server">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" width="100%">
<tr class="tr1" height="26">
<td class="TD_Title" align="center">
姓名 </td>
<td class="TD_Title" align="center">
性别
</td>
<td class="TD_Title" align="center">
年龄
</td>
<td class="TD_Title" align="center">
籍贯
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr onmouseover="this.bgColor='#F6F6F6'" onmouseout="this.bgColor='#FFFFFF'" height="26">
<td align="center">
<%#Eval("Name") %>
</td>
<td align="center">
<%#Eval ("Sex")%>
</td>
<td align="center">
<%#Eval("Age")%>
</td>
<td align="center">
<%#Eval("native")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<div class="hr10px">
</div>
<div class="page" id="ctrlRecordPage">
总<asp:Label ID="Zpage" runat="server" Text="1"></asp:Label>页/
<asp:Label ID="Zcount" runat="server" Text="0"></asp:Label>条
<asp:LinkButton ID="start_button" runat="server" OnClick="start_button_Click">首页</asp:LinkButton>
<asp:LinkButton ID="up_button" runat="server" OnClick="up_button_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="down_button" runat="server" OnClick="down_button_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="end_button" runat="server" OnClick="end_button_Click">末页</asp:LinkButton>
当前<asp:DropDownList ID="Pageing" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Pageing_SelectedIndexChanged">
</asp:DropDownList>
页
</div>
后台:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public partial class Analysis_BCXZ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataBird();
}
private void Pageing_databird(int value)
{
Pageing.Items.Clear();
int zdatapage = Convert.ToInt32(Zpage.Text);
if (zdatapage > 0)
{
for (int i = 1; i <= zdatapage; i++)
{
Pageing.Items.Add(i.ToString());
}
}
else
{
Pageing.Items.Add("1");
}
if (Pageing.Items.FindByValue(value.ToString()) != null) Pageing.SelectedValue = value.ToString();
if (zdatapage > 1)
{
Pageing.Enabled = true;
int datapageing = Convert.ToInt32(Pageing.SelectedValue);
if (datapageing > 1)
{
start_button.Enabled = true;
up_button.Enabled = true;
}
else
{
start_button.Enabled = false;
up_button.Enabled = false;
}
if (datapageing < zdatapage)
{
down_button.Enabled = true;
end_button.Enabled = true;
}
else
{
down_button.Enabled = false;
end_button.Enabled = false;
}
}
else
{
start_button.Enabled = false;
up_button.Enabled = false;
down_button.Enabled = false;
end_button.Enabled = false;
Pageing.Enabled = false;
}
}
protected void start_button_Click(object sender, EventArgs e)
{
int datapageing = 1;
if (Pageing.Items.FindByValue(datapageing.ToString()) != null) Pageing.SelectedValue = datapageing.ToString();
DataBird();
}
protected void up_button_Click(object sender, EventArgs e)
{
int datapageing = Convert.ToInt32(Pageing.SelectedValue);
if (datapageing > 1) datapageing--;
if (Pageing.Items.FindByValue(datapageing.ToString()) != null) Pageing.SelectedValue = datapageing.ToString();
DataBird();
}
protected void down_button_Click(object sender, EventArgs e)
{
int datapageing = Convert.ToInt32(Pageing.SelectedValue);
int zdatapage = Convert.ToInt32(Zpage.Text);
if (datapageing < zdatapage) datapageing++;
if (Pageing.Items.FindByValue(datapageing.ToString()) != null) Pageing.SelectedValue = datapageing.ToString();
DataBird();
}
protected void end_button_Click(object sender, EventArgs e)
{
int zdatapage = Convert.ToInt32(Zpage.Text);
if (Pageing.Items.FindByValue(zdatapage.ToString()) != null) Pageing.SelectedValue = zdatapage.ToString();
DataBird();
}
protected void Pageing_SelectedIndexChanged(object sender, EventArgs e)
{
DataBird();
}
protected void DataBird()
{
DataSet ds = new DataSet();
try
{
OleDbConnection conn = new OleDbConnection(@"Provider='Microsoft.Jet.OLEDB.4.0';Data Source='E:/工作/Website/UI_A/Special/data/367.mdb'");
OleDbDataAdapter da = new OleDbDataAdapter(" SELECT [CompanyName],[City],[Tel],[Month],[Province],[P_City] FROM [Default];", conn);
da.Fill(ds);
}
catch (Exception)
{
ds = null;
}
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
int datapagesize = 10;
pds.PageSize = datapagesize;
Zcount.Text = pds.DataSourceCount.ToString();
Zpage.Text = pds.PageCount.ToString();
int zpaging = 1;
if (Pageing.SelectedIndex > -1) zpaging = Convert.ToInt32(Pageing.SelectedValue);
Pageing_databird(zpaging);
pds.CurrentPageIndex = Convert.ToInt32(Pageing.SelectedValue) - 1;
rptPLHList.DataSource = pds;
rptPLHList.DataBind();
}
}