datagrid分页《非控件版》

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

DataGridPage1.SetTarget(MyDataGrid, New BindDataDelegate(AddressOf **_binddata _ ** ))
DataGridPage1.SetStyle(10, False)

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

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

HTML:

1@ Page Language="vb" AutoEventWireup="false" Codebehind="fenye.aspx.vb" Inherits="datagridtitle.fenye"
 1<html>
 2<head>
 3<title>fenye</title>
 4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 5<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body ms_positioning="GridLayout">
10<form id="Form1" method="post" runat="server">
11<font face="MS UI Gothic">
12<asp:datagrid allowpaging="True" height="224px" id="MyDataGrid" runat="server" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 112px" width="744px">
13<pagerstyle visible="False"></pagerstyle>
14</asp:datagrid>
15<asp:linkbutton height="8px" id="LinkButton1" runat="server" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 96px" width="48px">第一頁</asp:linkbutton>
16<asp:linkbutton id="LinkButton2" runat="server" style="Z-INDEX: 103; LEFT: 64px; POSITION: absolute; TOP: 96px" width="56px">前一頁</asp:linkbutton>
17<asp:linkbutton id="LinkButton3" runat="server" style="Z-INDEX: 104; LEFT: 120px; POSITION: absolute; TOP: 96px" width="48px">後一頁</asp:linkbutton>
18<asp:linkbutton id="LinkButton4" runat="server" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 96px" width="80px">最後一頁</asp:linkbutton>
19<asp:label id="Label1" runat="server" style="Z-INDEX: 106; LEFT: 552px; POSITION: absolute; TOP: 80px" width="56px">跳轉到:</asp:label>
20<asp:textbox height="24px" id="txtGoPage" runat="server" style="Z-INDEX: 107; LEFT: 624px; POSITION: absolute; TOP: 80px" width="48px"></asp:textbox>
21<asp:button height="24px" id="btnGo" runat="server" style="Z-INDEX: 108; LEFT: 680px; POSITION: absolute; TOP: 80px" text="Button" width="56px"></asp:button>
22<asp:label height="24px" id="lblCurrentIndex" runat="server" style="Z-INDEX: 109; LEFT: 296px; POSITION: absolute; TOP: 88px" width="120px">Label</asp:label>
23<asp:label id="lblPageCount" runat="server" style="Z-INDEX: 110; LEFT: 424px; POSITION: absolute; TOP: 88px" width="112px">Label</asp:label></font>
24</form>
25</body>
26</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 = "[

1<font color="blue">當前頁為:" &amp; (MyDataGrid.CurrentPageIndex + 1) &amp; "頁</font>

]"
lblPageCount.Text = "[

1<font color="blue">共:" &amp; MyDataGrid.PageCount &amp; "頁</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

完成;

Published At
Categories with Web编程
Tagged with
comments powered by Disqus