DataGrid分页代码

时间:2005/9/28 11:47:17      阅读:1225     附件: DataGrid分页代码.rar (经验值:0)     下载:10

做了个DataGrid分页代码,里面加了Mouse在行间移动时行背景色的变化,其它需要扩展的功能自己研究吧。

注:图中上面的分页为DataGrid自己带的分页,下面是自己定义的分页,可以按照你的需要选择一种分页方法。

page.aspx代码:

 
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Web3.cn--DataGrid分页显示演示页</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <style type="text/css"> BODY { COLOR: #333; LINE-HEIGHT: 150%; FONT-FAMILY: 'Lucida Grande','Lucida Sans Unicode','宋体','新宋体',arial,verdana,sans-serif } TD { FONT-SIZE: 9pt } </style> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <TABLE id="Table25" cellPadding="0" width="640" border="0" style="WIDTH: 640px; HEIGHT: 277px" cellSpacing="0"> <TR> <TD bgColor="gainsboro" height="30" colSpan="1" rowSpan="1"> <P align="left">&nbsp; <asp:Label id="LblNum" runat="server"></asp:Label></P> </TD> </TR> <TR> <TD vAlign="top"> <asp:datagrid id="dgdData" runat="server" Width="100%" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True" BorderColor="#CCCCCC" CellPadding="1"> <ItemStyle Wrap="False"></ItemStyle> <HeaderStyle Font-Bold="True" Wrap="False" CssClass="GridHeadBackColor" BackColor="DarkGray"> </HeaderStyle> <Columns> <asp:HyperLinkColumn Target="_blank" DataTextField="Title" HeaderText="标题" NavigateUrl="http://web3.cn"></asp:HyperLinkColumn> <asp:BoundColumn DataField="Ctime" HeaderText="创建时间" DataFormatString="{0:yy-MM-dd}"> </asp:BoundColumn> </Columns> <PagerStyle NextPageText="下一页" Font-Bold="True" PrevPageText="上一页" HorizontalAlign="Left" Position="Top" BackColor="#E8E7E7" PageButtonCount="20" Mode="NumericPages"></PagerStyle> </asp:datagrid> </TD> </TR> <tr> <td bgColor="gainsboro" height="30"> <asp:label id="lblCurrentIndex" runat="server"></asp:label> <asp:label id="lblPageCount" runat="server"></asp:label>&nbsp; <asp:linkbutton id="btnFirst" runat="server" onclick="PagerButtonClick" CommandArgument="0"> 首页</asp:linkbutton>&nbsp; <asp:linkbutton id="btnPrev" runat="server" onclick="PagerButtonClick" CommandArgument="prev"> 前一页</asp:linkbutton>&nbsp; <asp:linkbutton id="btnNext" runat="server" onclick="PagerButtonClick" CommandArgument="next"> 下一页</asp:linkbutton>&nbsp; <asp:linkbutton id="btnLast" runat="server" onclick="PagerButtonClick" CommandArgument="last"> 最后页</asp:linkbutton>&nbsp;转到第 <asp:TextBox id="TxtPager" runat="server" Width="22px"></asp:TextBox> <asp:Button id="CmdGo" runat="server" Text="Go"></asp:Button> </td> </tr> </TABLE> <br> 更多技术信息请浏览:<a href="http://web3.cn" target="_blank">http://web3.cn</a> </form> </body> </HTML>
page.aspx.vb代码:

 
   
'欢迎大家提出修改建议 '我的Email:hsdwf@163.com '本程序在本程序在Win2000[2003]+IE6.0+.Net FrameWork 1.1+IIS 5.0(IIS 6.0) 下测试通过。 '如有任何问题请与踢西[hsdwf@163.com]联系 '如果您有兴趣参与更多的.NET学习的工作,请与我联系或访问http://web3.cn Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If Not Page.IsPostBack Then '如果页面是第一次载入则... BindData() '绑定数据表到DataGrid(http://web3.cn) ShowStats() '检查分页状态(http://web3.cn) End If End Sub '绑定数据表到DataGrid(http://web3.cn) Sub BindData() Dim dv As DataView dv = GetNewsInfo().DefaultView LblNum.Text = "(共" & dv.Table.Rows.Count & "条信息)" dgdData.DataSource = dv Try dgdData.DataBind() Catch '防止分页错误(http://web3.cn) dgdData.CurrentPageIndex = dgdData.PageCount - 1 dgdData.DataBind() End Try End Sub '读取数据库,返回数据表(http://web3.cn) Public Function GetNewsInfo() As DataTable Dim sql As String sql = "select * from News order by ID desc" Dim ds As New DataSet Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Server.MapPath("db.mdb")) conn.Open() Dim da As New OleDbDataAdapter da.SelectCommand = New OleDbCommand da.SelectCommand.Connection = conn da.SelectCommand.CommandText = sql da.Fill(ds, "News") conn.Close() Return ds.Tables(0) End Function '===================================================================================================== '分页代码开始(http://web3.cn) Private Sub CmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdGo.Click If Trim(TxtPager.Text) = "" Then '输入框没输入数据时退出本函数 (http://web3.cn) Exit Sub End If If IsNumeric(TxtPager.Text) Then '检查输入的是否数字(http://web3.cn) If Trim(TxtPager.Text) > 0 Then dgdData.CurrentPageIndex = TxtPager.Text - 1 TxtPager.Text = "" BindData() ShowStats() Else TxtPager.Text = "" End If Else TxtPager.Text = "" End If End Sub '检查分页状态(http://web3.cn) Sub ShowStats() lblCurrentIndex.Text = "页次:<Font color=#FF0000>" + (dgdData.CurrentPageIndex + 1).ToString() + _ "</font>/<Font color=#FF0000>" + dgdData.PageCount.ToString() + "</font>" lblPageCount.Text = "每页<Font color=#FF0000>" + dgdData.PageSize.ToString() + "</font>行" If dgdData.PageCount = 1 Then btnFirst.Enabled = False btnPrev.Enabled = False btnNext.Enabled = False btnLast.Enabled = False Else If (dgdData.CurrentPageIndex >= (dgdData.PageCount - 1)) Then btnFirst.Enabled = True btnPrev.Enabled = True btnNext.Enabled = False btnLast.Enabled = False Else If dgdData.CurrentPageIndex <= 0 Then btnFirst.Enabled = False btnPrev.Enabled = False btnNext.Enabled = True btnLast.Enabled = True Else btnFirst.Enabled = True btnPrev.Enabled = True btnNext.Enabled = True btnLast.Enabled = True End If End If End If End Sub '点击按键代码 (http://web3.cn) Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs) Dim arg As String = sender.CommandArgument Select Case arg Case "next" '下一页(http://web3.cn) If (dgdData.CurrentPageIndex < (dgdData.PageCount - 1)) Then dgdData.CurrentPageIndex += 1 End If Case "prev" '前一页(http://web3.cn) If (dgdData.CurrentPageIndex > 0) Then dgdData.CurrentPageIndex -= 1 End If Case "last" '最后页(http://web3.cn) dgdData.CurrentPageIndex = (dgdData.PageCount - 1) Case Else dgdData.CurrentPageIndex = System.Convert.ToInt32(arg) End Select BindData() ShowStats() End Sub '分页代码结束(http://web3.cn) '===================================================================================================== Private Sub dgdData_ItemCreated(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdData.ItemCreated 'Mouse在行间移动时的行背景色(http://web3.cn) If (e.Item.ItemIndex >= 0) Then e.Item.Attributes.Add("onMouseOver", "javascript:this.bgColor='#cccccc';") 'Mouse指向时背景色 e.Item.Attributes.Add("onMouseOut", "javascript:this.bgColor='#ffffff';") 'Mouse移开时背景色 End If End Sub Private Sub dgdData_PageIndexChanged(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgdData.PageIndexChanged 'DataGrid自己带分页 dgdData.CurrentPageIndex = e.NewPageIndex() BindData() ShowStats() End Sub
最后的演示图如下:

评论
  • Re:DataGrid分页代码  (2009/3/20 16:45:58) by ylp1588 
    用DataGrid分页好呢还是存储过程分页好?
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.