谁可有代码,可以共赏一下吗?
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Label1.Text="1";
this.fill();
}
}
private void fill()
{
int pag=Convert.ToInt32(this.Label1.Text);//设置当前页
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=980123;");//实例化连接
SqlDataAdapter sda=new SqlDataAdapter();//建立一个数据适配器对象
sda.SelectCommand=new SqlCommand("select * from Employees",con);//实例化SelectCommand,并用他从数据库读出全部数据
DataSet ds=new DataSet();//定义一个数据集填充
sda.Fill(ds,"name");//使用适配器填充数据集到本地表“name”
PagedDataSource ps=new PagedDataSource();//实例化一个PagedDataSource,这个本来是封装是DATAGRID里面的
ps.DataSource=ds.Tables["name"].DefaultView;//设置他的数据源为ds.Tables["name"].DefaultView数据视图
ps.AllowPaging=true;//允许分页
ps.PageSize=3;//每页显示数量
ps.CurrentPageIndex=pag-1;//当前页码,因为页是从0开始的,所以要减1
this.Button1.Enabled=true;//按钮当前状态
this.Button2.Enabled=true;
this.Label5.Text=ps.PageCount.ToString();
if(pag==1)
{
this.Button1.Enabled=false;//如果当前页是 1 ,上一页按钮不可用
}
if(pag==ps.PageCount)
{
this.Button2.Enabled=false;//如果当前页是最后一页 ,下一页按钮不可用
}
this.Repeater1.DataSource=ps;
this.Repeater1.DataBind();
}
窗体代码
private void Button2_Click(object sender, System.EventArgs e)
{
this.Label1.Text=((Convert.ToInt32(this.Label1.Text))+1).ToString();
this.fill();
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.Label1.Text=((Convert.ToInt32(this.Label1.Text))-1).ToString();
this.fill();
}
把你的代码写出来参考一下。
这是我写的:
Sub binddata()
TotalPage = GetTotalPage()
If Request.QueryString("commandname") <> "" Then
Select Case Request.QueryString("commandname")
Case "first"
CurrentPage = 1
Case "prev"
CurrentPage = CInt(Session("CurrentPage")) - 1
If CurrentPage < 1 Then CurrentPage = 1
Case "next"
CurrentPage = CInt(Session("CurrentPage")) + 1
If CurrentPage > TotalPage Then CurrentPage = TotalPage
Case "last"
CurrentPage = TotalPage
End Select
Session("CurrentPage") = CurrentPage
ElseIf Not (Session("CurrentPage") Is Nothing) Then
CurrentPage = Session("CurrentPage")
Else
CurrentPage = 1
Session("CurrentPage") = CurrentPage
End If
Dim adp As New OleDbDataAdapter("select * from adder order by id ", conn)
Dim ds As New DataSet
StartPosition = (CurrentPage - 1) * PageSize
adp.Fill(ds, StartPosition, PageSize, "adder")
MyRepeater.DataSource = ds.Tables("adder").DefaultView
MyRepeater.DataBind()
theCurrentPage.Text = CurrentPage.ToString
theTotalPage.Text = TotalPage.ToString
End Sub
Function GetTotalPage()
Dim TotalRecord As Integer
Dim strsql As String = "select count(*) as total From adder"
Dim cmd As New OleDbCommand(strsql, conn)
conn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.Read() Then
TotalRecord = dr.Item("total")
Else
TotalRecord = 0
End If
conn.Close()
If (TotalRecord Mod PageSize) = 0 Then
GetTotalPage = TotalRecord / PageSize
Else
GetTotalPage = TotalRecord \ PageSize + 1
End If
End Function