datagrid分页《非控件版》

类别:.NET开发 点击:0 评论:0 推荐:

首先对我之前的发表的那篇补充一下:当你在你的建立的工程中要用到我做的那个用户控件的话:声明 Protected DataGridPage1 As DataGridPage,前是你拖进来的控件名,后是你定义用户控件。然后在你代码中要用你的控件就写上:

DataGridPage1.SetTarget(MyDataGrid, New BindDataDelegate(AddressOf binddata))
DataGridPage1.SetStyle(10, False)

下划线部分是你自己写的一个绑定之类的涵数。SUB,FUNCTION 等。

以下是我不做成控件时的代码:

HTML:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="fenye.aspx.vb" Inherits="datagridtitle.fenye"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>fenye</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 MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="MS UI Gothic">
    <asp:DataGrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 112px"
     runat="server" Width="744px" Height="224px" AllowPaging="True">
     <PagerStyle Visible="False"></PagerStyle>
    </asp:DataGrid>
    <asp:LinkButton id="LinkButton1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 96px"
     runat="server" Width="48px" Height="8px">第一頁</asp:LinkButton>
    <asp:LinkButton id="LinkButton2" style="Z-INDEX: 103; LEFT: 64px; POSITION: absolute; TOP: 96px"
     runat="server" Width="56px">前一頁</asp:LinkButton>
    <asp:LinkButton id="LinkButton3" style="Z-INDEX: 104; LEFT: 120px; POSITION: absolute; TOP: 96px"
     runat="server" Width="48px">後一頁</asp:LinkButton>
    <asp:LinkButton id="LinkButton4" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 96px"
     runat="server" Width="80px">最後一頁</asp:LinkButton>
    <asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 552px; POSITION: absolute; TOP: 80px" runat="server"
     Width="56px">跳轉到:</asp:Label>
    <asp:TextBox id="txtGoPage" style="Z-INDEX: 107; LEFT: 624px; POSITION: absolute; TOP: 80px"
     runat="server" Width="48px" Height="24px"></asp:TextBox>
    <asp:Button id="btnGo" style="Z-INDEX: 108; LEFT: 680px; POSITION: absolute; TOP: 80px" runat="server"
     Height="24px" Width="56px" Text="Button"></asp:Button>
    <asp:Label id="lblCurrentIndex" style="Z-INDEX: 109; LEFT: 296px; POSITION: absolute; TOP: 88px"
     runat="server" Height="24px" Width="120px">Label</asp:Label>
    <asp:Label id="lblPageCount" style="Z-INDEX: 110; LEFT: 424px; POSITION: absolute; TOP: 88px"
     runat="server" Width="112px">Label</asp:Label></FONT>
  </form>
 </body>
</HTML>

WEB代码:

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' ページを初期化するユーザー コードをここに挿入します。
        If Not IsPostBack Then

            ViewState("strSort") = "orderid"

            MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

            MyDataGrid.DataBind()
            ShowStatsPage()
            'Call orderbind()
        End If
    End Sub
    Private Function GetDv(ByVal strSort As String) As DataView

 

        Dim dv As DataView

        Dim CN As New SqlConnection

        Try

 

            CN.ConnectionString = "data source=yangchangquan;initial catalog=Northwind;persist security info=False;user id=sa;Password=pass;"

            CN.Open()

 

            Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from orders", CN)

            Dim ds As New DataSet

            adp.Fill(ds)


           
            dv = ds.Tables(0).DefaultView

        Catch ex As Exception

#If DEBUG Then

            Session("Error") = ex.ToString()

            Response.Redirect("../error.aspx")

#End If

        Finally

            '???接

            CN.Close()

        End Try

        '排序

        dv.Sort = strSort

        Return dv

    End Function

 


    Private Sub MyDataGrid_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
        MyDataGrid.CurrentPageIndex = 0

        '得到排序的列

        ViewState("strSort") = e.SortExpression.ToString()

        MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

        MyDataGrid.DataBind()

        ShowStatsPage()
    End Sub

 


    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Dim pageI As Integer
        If (txtGoPage.Text <> "") Then
            pageI = CInt(Trim(txtGoPage.Text)) - 1
            If (pageI >= 0 And pageI < (MyDataGrid.PageCount)) Then
                MyDataGrid.CurrentPageIndex = pageI
            End If
        End If
        ViewState("strSort") = "orderid"

        MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

        MyDataGrid.DataBind()
        ShowStatsPage()
    End Sub

Private Sub ShowStatsPage()
        lblCurrentIndex.Text = "[<font color='blue'>當前頁為:" & (MyDataGrid.CurrentPageIndex + 1) & "頁</font>]"
        lblPageCount.Text = "[<font color='blue'>共:" & MyDataGrid.PageCount & "頁</font>]"

    End Sub

 Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        MyDataGrid.CurrentPageIndex = 0
        ViewState("strSort") = "orderid"

        MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

        MyDataGrid.DataBind()
        ShowStatsPage()
    End Sub

    Private Sub LinkButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
        ViewState("strSort") = "orderid"

        MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

        MyDataGrid.DataBind()
        ShowStatsPage()
    End Sub

    Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        If (MyDataGrid.CurrentPageIndex > 0) Then
            MyDataGrid.CurrentPageIndex = MyDataGrid.CurrentPageIndex - 1
            ViewState("strSort") = "orderid"

            MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

            MyDataGrid.DataBind()
            ShowStatsPage()
        End If
    End Sub

    Private Sub LinkButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
        If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
            MyDataGrid.CurrentPageIndex = MyDataGrid.CurrentPageIndex + 1
            ViewState("strSort") = "orderid"

            MyDataGrid.DataSource = GetDv(ViewState("strSort").ToString())

            MyDataGrid.DataBind()
            ShowStatsPage()
        End If
    End Sub

完成;

本文地址:http://com.8s8s.com/it/it44991.htm