位置:海鸟网 > IT > ASP.NET >

开心就贴(一)

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>


<s cript language="VB" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
    BindGrid
End Sub

Sub MyDataGrid_Page(sender As Object, e As DataGridPageChangedEventArgs)
    startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
    BindGrid
    ShowStats
End Sub

Sub BindGrid()

Dim DS As DataSet
Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataSetCommand

MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
MyCommand = New SQLDataSetCommand("select * from Authors", MyConnection)

DS = new DataSet()
MyCommand.FillDataSet(ds, "Authors")

MyDataGrid.DataSource=ds.Tables("Authors").DefaultView
MyDataGrid.DataBind()


ShowStats
End Sub

Sub PagerButtonClick(sender As Object, e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument

Select arg
    Case "next":
        If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
            MyDataGrid.CurrentPageIndex += 1
        End If
Case "prev":
        If (MyDataGrid.CurrentPageIndex > 0) Then
            MyDataGrid.CurrentPageIndex -= 1
        End If
Case "last":
        MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
Case Else:
    'page number
        MyDataGrid.CurrentPageIndex = arg.ToInt32()
End Select

BindGrid
ShowStats
End Sub

Sub ShowStats()
lblCurrentIndex.Text = "CurrentPageIndex is " & MyDataGrid.CurrentPageIndex
lblPageCount.Text = "PageCount is " & MyDataGrid.PageCount
End Sub
</s cript>



    <form runat="server">
   
<ASP:DataGrid id="MyDataGrid" runat="server"
AllowPaging="True"
PageSize="5"
PageCount="1"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="MyDataGrid_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
/>
    </form>

<p>

<asp:LinkButton id="btnFirst" runat="server"
Text="Go to the first page"
CommandArgument="0"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>

<asp:LinkButton id="btnPrev" runat="server"
Text="Previous page"
CommandArgument="prev"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>

<asp:LinkButton id="btnNext" runat="server"
Text="Next page"
CommandArgument="next"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>

<asp:LinkButton id="btnLast" runat="server"
Text="Go to the last page"
CommandArgument="last"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>

<p>

<table bgcolor="#eeeeee" cellpadding="6"><tr><td nowrap><font face="Verdana" size="-2">

<asp:Label id="lblCurrentIndex" runat="server" /><br>
<asp:Label id="lblPageCount" runat="server" /><br>