以下是小弟摘出的代码。想实现分页处理。用datagrid显示数据。但是当页面处于page1时候,点next(下一页)到page2时候,需要点2下,同理,页面处于last时候,点prev(上一页)也是需要两下。其他的没问题。
小弟找的实在喷火了~~>_<~~希望各位有同样经历的来顶,高手指点江山。
html的代码如下。<asp:datagrid Width="100%" runat="server" id="dgout" AllowSorting="True" OnSortCommand="dgsortpage"
headerstyle-backcolor="#aaaadd" BorderWidth="0" alternatingitemstyle-backcolor="#eeeeee" bordercolor="black"
cellpadding="2" cellspacing="0" allowpaging="true" pagerstyle-mode="numericpages" pagesize="3"
onpageindexchanged="changepage" pagerstyle-horizontalalign="right" autogeneratecolumns="false">
<columns>
<asp:boundcolumn datafield="序号" ItemStyle-Width="3.5%" headertext="序号" HeaderStyle-Wrap="False" itemstyle-horizontalalign="right" />
<asp:hyperlinkcolumn datatextfield="产品名称" headertext="产品名称" HeaderStyle-Wrap="False" datanavigateurlfield="产品名称"
datanavigateurlformatstring="students.aspx?id=(0)" target="_blank" />
<asp:boundcolumn datafield="发布时间" headertext="发布时间" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="生产企业" headertext="生产企业" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="单位" headertext="单位" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="市场售价" headertext="市场售价" itemstyle-horizontalalign="right" HeaderStyle-Wrap="False"
itemstyle-width="8%" />
</columns>
</asp:datagrid>
vb代码如下。:
Public Sub oleconn()        Dim constr As String        constr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=cbcp;Data Source=CSSC-GL;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CSSC-GL;Use Encryption for Data=False;Tag with column collation when possible=False"
        conn = New OleDbConnection(constr)
        conn.Open()
    End Sub
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnFirst.Text = "最首页"
        btnPrev.Text = "前一页"
        btnNext.Text = "下一页"
        btnLast.Text = "最后页"        If Not IsPostBack Then
            '-------------------------------------------清空查询栏目---------
            'if ispostback then txtfind.text=""            oleconn()
            bindgrid()        End If
   End Sub
    Sub changepage(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
        dgout.CurrentPageIndex = e.NewPageIndex
        oleconn()
        bindgrid()
        ShowStats()
    End Sub
    Private Sub bindgrid()
        Dim adpt As OleDbDataAdapter
        Dim ds As DataSet
        Dim consql As String
        consql = "select * from cbcpdb"
        adpt = New OleDbDataAdapter(consql, conn)        ds = New DataSet
        adpt.Fill(ds, "cbcpdb")
        ds.Tables("cbcpdb").DefaultView.Sort = sortfield.SelectedItem.Text & sorttype.Text        dgout.DataSource = ds.Tables("cbcpdb").DefaultView
        dgout.DataBind()        conn.Close()    End Sub
    Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        'used by external paging UI
        Dim arg As String = sender.CommandArgument
        On Error Resume Next        Select Case arg
            Case "next"
                If (dgout.CurrentPageIndex < (dgout.PageCount - 1)) Then
                    dgout.CurrentPageIndex += 1
                End If
            Case "prev"
                If (dgout.CurrentPageIndex > 0) Then
                    dgout.CurrentPageIndex -= 1
                End If
            Case "last"
                dgout.CurrentPageIndex = (dgout.PageCount - 1)
            Case Else
                'page number
                dgout.CurrentPageIndex = System.Convert.ToInt32(arg)
        End Select
        bindgrid()
        ShowStats()
    End Sub
    Private Sub ShowStats()
        lblCurrentIndex.Text = "您现在看的是第 " + (dgout.CurrentPageIndex + 1).ToString() + " 页"
        lblPageCount.Text = "当前总共 " + dgout.PageCount.ToString() + " 页"
    End Sub
    Sub subsort_click(ByVal sender As Object, ByVal e As EventArgs)
        oleconn()
    End Sub