个性化的分页实例
来源:网络整理 责任编辑:栏目编辑 发表时间:2013-07-01 16:42 点击:次
注意:本文的源程序代码用VB和C#同时进行说明。
我们所知道的分页程序一般都是简单地通过修改DataGrid的属性来实现分页,这样做的最大好处就是简单,用户根本不用知道分页是如何产生的。但它同样有缺点,不能按照用户的需要产生相应的样式。要得到个性化的分页,就必须自己动手编写代码。下面来看一个功能更为强大的分页实例——个性化的分页功能实现。
完整的程序代码如下:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<html>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object,e As EventArgs)
'判断是否隐藏PagerStyle-Mode
if (chk1.Checked) then
Grid1.PagerStyle.Visible=true
else
Grid1.PagerStyle.Visible=false
end if
BindGrid
End Sub
Sub BindGrid()
Dim Provider,DataBase,ConnStr,SQL As String
'第一步: 进行数据库的链接
Provider="Microsoft.Jet.OLEDB.4.0;"
DataBase=Server.MapPath("Sample.mdb")
ConnStr="Provider="+Provider+"Data Source="+DataBase
'第二步:执行SQL指令,选出记录集合
Dim Cmd As OleDbDataAdapter
Cmd=New OleDbDataAdapter("Select * From 学生成绩表",ConnStr)
Dim ds As DataSet=New DataSet()
Cmd.Fill(ds,"学生成绩表")
'第三步:将数据集合同DataGrid结合在一起
Grid1.DataSource=ds.Tables("学生成绩表").DefaultView
Grid1.DataBind()
ShowStats()
End Sub
Sub PagerButtonClick(Sender As Object,e As EventArgs)
'获得LinkButton的参数值
Dim arg As String =CType(Sender,LinkButton).CommandArgument
Select Case arg
case ("next")
if (Grid1.CurrentPageIndex < (Grid1.PageCount - 1)) then
Grid1.CurrentPageIndex +=1
end if
case ("prev")
if (Grid1.CurrentPageIndex > 0) then
Grid1.CurrentPageIndex -=1
end if
case ("last")
Grid1.CurrentPageIndex = (Grid1.PageCount - 1)
case else
'本页值
Grid1.CurrentPageIndex = CInt(arg)
End Select
BindGrid
End Sub
Sub Grid1_Page(sender As Object, e As DataGridPageChangedEventArgs)
'处理按下数字的方法
BindGrid
End Sub
Sub ShowStats()
'显示页面信息
lblCurrentIndex.Text = "当前页数为: " &(Grid1.CurrentPageIndex+1)
lblPageCount.Text = "总页数是: " & Grid1.PageCount
End Sub
</script>
<body>
<h3><font face="Verdana">个性化的分页实例</font></h3>
<form runat=server>
<ASP:DataGrid id="Grid1" runat="server"
AllowPaging="True"
PageSize="5"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="Grid1_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="true">
</AS
我们所知道的分页程序一般都是简单地通过修改DataGrid的属性来实现分页,这样做的最大好处就是简单,用户根本不用知道分页是如何产生的。但它同样有缺点,不能按照用户的需要产生相应的样式。要得到个性化的分页,就必须自己动手编写代码。下面来看一个功能更为强大的分页实例——个性化的分页功能实现。
完整的程序代码如下:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<html>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object,e As EventArgs)
'判断是否隐藏PagerStyle-Mode
if (chk1.Checked) then
Grid1.PagerStyle.Visible=true
else
Grid1.PagerStyle.Visible=false
end if
BindGrid
End Sub
Sub BindGrid()
Dim Provider,DataBase,ConnStr,SQL As String
'第一步: 进行数据库的链接
Provider="Microsoft.Jet.OLEDB.4.0;"
DataBase=Server.MapPath("Sample.mdb")
ConnStr="Provider="+Provider+"Data Source="+DataBase
'第二步:执行SQL指令,选出记录集合
Dim Cmd As OleDbDataAdapter
Cmd=New OleDbDataAdapter("Select * From 学生成绩表",ConnStr)
Dim ds As DataSet=New DataSet()
Cmd.Fill(ds,"学生成绩表")
'第三步:将数据集合同DataGrid结合在一起
Grid1.DataSource=ds.Tables("学生成绩表").DefaultView
Grid1.DataBind()
ShowStats()
End Sub
Sub PagerButtonClick(Sender As Object,e As EventArgs)
'获得LinkButton的参数值
Dim arg As String =CType(Sender,LinkButton).CommandArgument
Select Case arg
case ("next")
if (Grid1.CurrentPageIndex < (Grid1.PageCount - 1)) then
Grid1.CurrentPageIndex +=1
end if
case ("prev")
if (Grid1.CurrentPageIndex > 0) then
Grid1.CurrentPageIndex -=1
end if
case ("last")
Grid1.CurrentPageIndex = (Grid1.PageCount - 1)
case else
'本页值
Grid1.CurrentPageIndex = CInt(arg)
End Select
BindGrid
End Sub
Sub Grid1_Page(sender As Object, e As DataGridPageChangedEventArgs)
'处理按下数字的方法
BindGrid
End Sub
Sub ShowStats()
'显示页面信息
lblCurrentIndex.Text = "当前页数为: " &(Grid1.CurrentPageIndex+1)
lblPageCount.Text = "总页数是: " & Grid1.PageCount
End Sub
</script>
<body>
<h3><font face="Verdana">个性化的分页实例</font></h3>
<form runat=server>
<ASP:DataGrid id="Grid1" runat="server"
AllowPaging="True"
PageSize="5"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="Grid1_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="true">
</AS
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>