【问题标题】:Can You "Assign" the PageCount Property of an ASP.NET GridView?您可以“分配”ASP.NET GridView 的 PageCount 属性吗?
【发布时间】:2008-11-12 22:37:46
【问题描述】:

我有一个 Web 应用程序正在使用具有自己内置分页的数据存储。 PagedResult 类告诉我总页数。我想做什么(在绑定我的 ASP.NET GridView 之后)这样做:

MyGridView.PageCount = thePageCount;

然后让 GridView 神奇地构建分页链接,就像它自己做的事情一样。

问题是“PageCount”是一个只读属性……那么,我怎样才能简单地做到这一点?

【问题讨论】:

    标签: asp.net gridview pagination


    【解决方案1】:

    要使用内置分页,GridView 与数据源交互。 GridView 有一个可设置的 PageSize 属性。

    如果使用 ObjectDataSource,则需要同时配置 SelectMethod 和 SelectCountMethod。您可以修改 PagedResult 类以返回记录计数而不是页计数,或者将 PagedResult 调用包装在将页计数转换为记录计数 (PageCount * PageSize) 的方法中。

    如果您的 PagedResult 类只是为了支持 Web 应用程序而存在,您应该考虑将其修改为更像典型的分页数据源。

    【讨论】:

      【解决方案2】:

      您可以创建自己的类来扩展 GridView 并覆盖 PageCount getter 方法以从 PagedResult 类返回值。

      【讨论】:

      • 我已经反映了 GridView,它实际上使用了一个私有字段......所以覆盖公共“PageCount”字段实际上是行不通的。好主意。
      【解决方案3】:

      使用 ObjectDataSource 控件,将其绑定到您的 GridView,并为 SelectCoutnMethod 属性设置处理程序。您可能必须为您的类编写小的包装对象,以检索与 ObjectDataSource 控件接口的数据。

      一些链接可以帮助你:

      ObjectDataSource Web Server Control Overview
      ObjectDataSource Class

      【讨论】:

        【解决方案4】:
               Dim myCount as Integer = 1 'this sets the page count to 1 
               While (oreader.Read())
                    myCount += 1 'increments once for everytime a item is counted
                    'this sets an array for the items to go into
                    idFname = oreader.GetOrdinal("workCenter")
                    'this retrieves the values at those indices
                    fName = oreader.GetValue(idFname)
                    BulletedList1.Items.Add(fName)
                End While
        
            Catch ex As Exception
                BulletedList1.Items.Add("No Workcenters Found")
            Finally
                oreader.Close()
                oconn.Close()
            End Try
        End If
        Me.insertItemForm.PagerSettings.PageButtonCount = myCount 'sets the page count to number of items in gridview or formview etc.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多