none
求助这种情况如何对gridview进行分页? RRS feed

  • 问题

  • 新建一网页,添加一GridView1和一Button1,并手动设置了GRIDVIEW1的列(a,b,c,列头为a,b,c),设定其打开分页功能,每页显示10行 Button1_Click为GRIDVIEW添加并显示了数据,GRIDVIEW1也显示了分页链接,但是点击链接并不能到达下一页,提示: System.Web.HttpException: GridView“GridView1”激发了未处理的事件“PageIndexChanging”。 请问:这种情况下,怎么能实现分页功能?? Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim currentTable As New DataTable Dim row As DataRow Dim i As Integer, j As Integer For i = 0 To 100 row = currentTable.NewRow() currentTable.Rows.InsertAt(row, i) Next GridView1.DataSource = currentTable GridView1.DataBind() For i = 0 To GridView1.Rows.Count - 1 For j = 0 To GridView1.Columns.Count - 1 GridView1.Rows(i).Cells(j).Text = (i + 1) * (j + 1) Next Next End Sub
    2009年4月4日 8:09

答案

  • 完整的例子

    <%@ Page Language="VB" %>
    
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        GridView1.DataSource = dbind()
        GridView1.DataBind()
      End Sub
    
      Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = dbind()
        GridView1.DataBind()
      End Sub
      
      Function dbind() As DataTable
        Dim currentTable As New DataTable
        Dim row As DataRow
        Dim i As Integer, j As Integer
        currentTable.Columns.Add(New System.Data.DataColumn("学生班级", GetType(System.String)))
        For i = 0 To 100
          row = currentTable.NewRow()
          row(0) = "班级" + i.ToString()
          currentTable.Rows.InsertAt(row, i)
        Next
        Return currentTable
      End Function
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging">
      </asp:GridView>
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
      </form>
    </body>
    </html>
    

    孟宪会
    2009年4月4日 10:47
    版主

全部回复

  • 完整的例子

    <%@ Page Language="VB" %>
    
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        GridView1.DataSource = dbind()
        GridView1.DataBind()
      End Sub
    
      Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = dbind()
        GridView1.DataBind()
      End Sub
      
      Function dbind() As DataTable
        Dim currentTable As New DataTable
        Dim row As DataRow
        Dim i As Integer, j As Integer
        currentTable.Columns.Add(New System.Data.DataColumn("学生班级", GetType(System.String)))
        For i = 0 To 100
          row = currentTable.NewRow()
          row(0) = "班级" + i.ToString()
          currentTable.Rows.InsertAt(row, i)
        Next
        Return currentTable
      End Function
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging">
      </asp:GridView>
      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
      </form>
    </body>
    </html>
    

    孟宪会
    2009年4月4日 10:47
    版主
  • 版主,你太强大了.多谢啦!
    2009年4月5日 0:23