DataGrid的使用(1)

Posted on 2004-11-15 16:18  生活即技术  阅读(426)  评论(0)    收藏  举报

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Data.WebForm1"%>
<HTML>
    
<body>
        
<form runat="server">
            
For our recordcount and pagecount
            
<asp:Label id="lblPageCount" runat="server" /><br>
            
<asp:DataGrid id="Data1" AllowPaging="True" OnPageIndexChanged="DataGrid_PageIndex" AutoGenerateColumns="False"
                runat="server" Width="536px">
                
<Columns>
                
<asp:BoundColumn DataField="id"></asp:BoundColumn>
                
<asp:HyperLinkColumn   DataNavigateUrlFormatString="aspx?id={0}" DataNavigateUrlField="uname" DataTextField="uname"  HeaderText="用户名" ></asp:HyperLinkColumn>
                
</Columns>
                
<PagerStyle  Width="100%"  NextPageText="下一页 " PrevPageText="上一页" PageButtonCount="5" Mode="NumericPages"></PagerStyle>
            
</asp:DataGrid>
            
        
</form>
    
</body>
</HTML>
后台代码
Public Class WebForm1
    
Inherits System.Web.UI.Page

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

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

    
End Sub

    
Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
    
Protected WithEvents Pubs As System.Web.UI.WebControls.DataGrid
    
Protected WithEvents ps As System.Web.UI.WebControls.DropDownList
    
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
Protected WithEvents Data1 As System.Web.UI.WebControls.DataGrid

    
'注意: 以下占位符声明是 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
    
Dim totalcount As Int32
    
Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
        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 top 100 id,uname  from user_login "
        Try
            Myconnection 
= New SqlClient.SqlConnection(connstr)
            Myconnection.Open()
            MyAdatper 
= New SqlClient.SqlDataAdapter(sql, Myconnection)
            MyAdatper.Fill(ds, 
"User_login")
            totalcount 
= ds.Tables("user_login").Rows.Count.ToString
            lblPageCount.Text 
= totalcount
            Data1.DataSource 
= ds.Tables("user_login")
            Data1.DataBind()
        
Catch ecc As Exception
            Response.
Write(ecc.Message)
            Response.
End()
        
Finally
        
End Try
        Response.
Write("连接成功")
    
End Sub

    
Sub DataGrid_PageIndex(ByVal sender As ObjectByVal e As DataGridPageChangedEventArgs)
        Data1.CurrentPageIndex 
= e.NewPageIndex
    
End Sub

End Class