asp.net中用reptear控件如何分页,给出vb源代码有分,散分了,来者有分,顶者有分
asp.net中用reptear控件如何分页,给出vb源代码有分,来者有分,顶者有分
给出C#代码的没分
[解决办法]
<% @ Page Language= "C# " %>
<% @ Import Namespace= "System.Data " %>
<% @ Import Namespace= "System.Data.OleDb " %>
<Script Language= "C# " Runat= "Server ">
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+Server.MapPath( ". ")+ "..\\DataBase\\db1.mdb; ";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState[ "PageCount "] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score ";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr[ "co "].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score ";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize, "Score ");
return ds.Tables[ "Score "].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState[ "PageIndex "];
PageCount = (int)ViewState[ "PageCount "];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next ":
if(CurrentPage <(PageCount-1)) CurrentPage++;
break;
case "prev ":
if(CurrentPage> 0) CurrentPage--;
break;
}
ViewState[ "PageIndex "] = CurrentPage;
ListBind();
}
</script>
<html>
<head>
<title> </title>
</head>
<body>
<form runat= "server ">
共有 <asp:Label id= "lblRecordCount " ForeColor= "red " runat= "server " /> 条记录
当前为 <asp:Label id= "lblCurrentPage "
ForeColor= "red " runat= "server " /> / <asp:Label id= "lblPageCount " ForeColor= "red " runat= "server " /> 页
<asp:DataList id= "score " runat= "server "
HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "Gainsboro "
EditItemStyle-BackColor= "yellow "
>
<ItemTemplate>
姓名: <%# DataBinder.Eval(Container.DataItem, "Name ") %>
<asp:LinkButton id= "btnSelect " Text= "编辑 " CommandName= "edit " runat= "server " />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id= "lbnPrevPage " Text= "上一页 " CommandName= "prev " OnCommand= "Page_OnClick " runat= "server " />
<asp:LinkButton id= "lbnNextPage " Text= "下一页 " CommandName= "next " OnCommand= "Page_OnClick " runat= "server " />
</form>
</body>
[解决办法]
我有!!!
Imports system.data
Imports system.data.sqlclient
Partial Class usermanage
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Dim current As Integer '记录着目前在哪一页上
Dim max As Integer '总共有多少页
Const rowCount As Integer = 5 '一页有多少行
Dim rowSum As Integer '总共有多少行
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim mysql As data = New data
Dim a1 As ArrayList = New ArrayList
ds = mysql.binddata( "bind_users ", a1)
'获取总共有多少行
Try
'获取总共有多少行
rowSum = ds.Tables(0).Rows.Count
Catch ex As Exception
rowSum = 0
End Try
'如果没有数据,退出过程
If rowSum = 0 Then Exit Sub
'计算出浏览数据的总页数
If rowSum Mod rowCount > 0 Then
'有余数要加1
max = rowSum \ rowCount + 1
Else
'正好除尽
max = rowSum \ rowCount
End If
current = 1
'调用绑定数据过程
readpage(current)
BindData()
maxpage.Text = max
End If
End Sub
'创建一个绑定数据的过程
Sub BindData()
Repeater1.DataSource = ds
Repeater1.DataBind()
currentpage.Text = current
bindimage()
End Sub
'创建一个填充数据集的过程
Sub readpage(ByVal n As Integer)
Dim mysql As data = New data
Dim a1 As ArrayList = New ArrayList
Dim cm As SqlCommand = mysql.command( "bind_users ", a1)
Dim da As New SqlDataAdapter(cm)
ds = New DataSet
ds.Clear()
da.Fill(ds, (n - 1) * rowCount, rowCount, "users ")
bindimage()
End Sub
Sub firstpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
current = 1
readpage(current)
BindData()
End Sub
Sub nextpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If currentpage.Text < maxpage.Text Then
current = currentpage.Text + 1
readpage(current)
BindData()
End If
End Sub
Sub previewpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If 1 < currentpage.Text Then
current = currentpage.Text - 1
readpage(current)
BindData()
End If
End Sub
Sub endpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
current = maxpage.Text
readpage(current)
BindData()
End Sub