惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
IT之家
IT之家
V
Visual Studio Blog
The Cloudflare Blog
博客园 - 司徒正美
Jina AI
Jina AI
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
S
SegmentFault 最新的问题
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - Franky
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 生活即技术

好久不更新了,忙的惭愧哦,思考一个应用 利用dataTable 排序的解决方法 项目中遇到的一些小问题及解决方法 常用代码 一个com+消息队列的例子 部署Com+ 2004我的爱,2005我的期待 加密解密相关 生成略缩图 论坛在线人数统计代码 上礼拜六犯的愚蠢错误 正则表达式(1) 又一新的分页方法(转帖) 用户控件的使用(二) 用户控件的使用(一) DataList的使用(二)自定义分页的实现 DataList的使用(1) DataGrid的使用(1)
DataGrid的使用(2)自定义分页
生活即技术 · 2004-11-16 · via 博客园 - 生活即技术

DataGrid.aspx页

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGrid_page.aspx.vb" Inherits="Data.DataGrid_page"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<title>DataGrid_page</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">
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<table>
                
<tr>
                    
<td style="HEIGHT: 5px">
                        
<asp:DataGrid id="DataGrid1" AllowCustomPaging="false" PageSize="10" DataKeyField="id" AllowPaging="True"
                            AutoGenerateColumns="False" runat="server" Width="504px">
                            
<Columns>
                                
<asp:TemplateColumn  HeaderText="编号"  > 
                                
<ItemTemplate ><b><a  href='<%# DataBinder.Eval(Container.DataItem, "id","aspx?id={0}") %>'>编号</a></b></ItemTemplate>
                                </asp:TemplateColumn> 
                                
<asp:BoundColumn DataField="uname" HeaderText="用户名"></asp:BoundColumn>
                            
</Columns>
                            
<PagerStyle Visible="False"></PagerStyle>
                        
</asp:DataGrid>
                    
</td>
                
</tr>
                
<tr>
                    
<td>
                        
<table width="500">
                            
<tr>
                                
<td>
                                    
<asp:linkbutton Runat="server" ID="frist" OnCommand="PagerButtonClick" CommandArgument="Frist">首页</asp:linkbutton>
                                    
<asp:linkbutton Runat="server" ID="prePage" OnCommand="PagerButtonClick" CommandArgument="pre">上一页</asp:linkbutton>
                                    
<asp:linkbutton Runat="server" ID="nextPage" OnCommand="PagerButtonClick" CommandArgument="next">下一页</asp:linkbutton>
                                    
<asp:linkbutton Runat="server" ID="lastPage" OnCommand="PagerButtonClick" CommandArgument="last">尾页</asp:linkbutton>
                                
</td>
                            
</tr>
                        
</table>
                    
</td>
                
</tr>
            
</table>
        
</form>
    
</body>
</HTML>

DataGrid.aspx.vb页

Public Class DataGrid_page
    
Inherits System.Web.UI.Page

#Region 
" Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    
End Sub

    
Protected WithEvents Data As System.Web.UI.WebControls.DataGrid
    
Protected WithEvents a As System.Web.UI.WebControls.DataGrid
    
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    
Protected WithEvents frist As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents prePage As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents nextPage As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents lastPage As System.Web.UI.WebControls.LinkButton

    
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    
Private Sub Page_Init(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Init
        
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    
End Sub


#
End Region

    
Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
        If Not IsPostBack Then
            DataBlind()
        
End If
    
End Sub


    
Sub DataBlind()
        
Dim Myconnection As SqlClient.SqlConnection
        
Dim MyAdatper As SqlClient.SqlDataAdapter
        
Dim ds As New DataSet
        
Dim read As SqlClient.SqlDataReader
        
Dim connstr = "server=(local);uid=sa;pwd=;database=achem56;"
        Dim sql As String = "select  id,uname  from user_login "
        Try
            Myconnection 
= New SqlClient.SqlConnection(connstr)
            MyAdatper 
= New SqlClient.SqlDataAdapter(sql, Myconnection)
            MyAdatper.Fill(ds, 
"User_login")
            DataGrid1.DataSource 
= ds.Tables("user_login")
            DataGrid1.DataBind()
        
Catch ecc As Exception
            Response.
Write(ecc.Message)
            Response.
End()
        
Finally
        
End Try
        Response.
Write("CurrentPageindex/totalPageIndex: " & DataGrid1.CurrentPageIndex + 1 & "/" & DataGrid1.PageCount)
    
End Sub

    
Sub PagerButtonClick(ByVal sender As ObjectByVal e As CommandEventArgs)
        
Select Case e.CommandArgument
            
Case "Frist"
                DataGrid1.CurrentPageIndex = 0
            
Case "pre"
                If DataGrid1.CurrentPageIndex > 0 Then
                    DataGrid1.CurrentPageIndex 
= DataGrid1.CurrentPageIndex - 1
                
End If
            
Case "next"
                If DataGrid1.CurrentPageIndex < DataGrid1.PageCount - 1 Then
                    DataGrid1.CurrentPageIndex 
= DataGrid1.CurrentPageIndex + 1
                
End If
            
Case "last"
                DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1
        
End Select
        DataBlind()
    
End Sub


    
Private Sub DataGrid1_PageIndexChanged(ByVal source As ObjectByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex 
= e.NewPageIndex
        DataBlind()
    
End Sub

End Class

代码其实是不复杂的,重点是理解DataGrid的处理过程,事件机制.