【问题标题】:Working with ASP.Net GridView sorting and paging使用 ASP.Net GridView 排序和分页
【发布时间】:2012-11-29 02:39:41
【问题描述】:

我们设置了一个 ASP.Net GridView 并包括排序和分页。当用户单击 GridView 列标题链接以对数据进行排序或用户单击 GridView 底部的数字链接以对数据进行分页时,没有任何反应。

这是 GridView 标记的精简版:

<asp:UpdatePanel 
    ID="UpdatePanelSummary" 
    runat="server" 
    UpdateMode="Always">

    <ContentTemplate> 

        <h1>Maintenance</h1>

        <% '-- GridView (Grid) for summary.                                                      -- %>
        <% '-- The user chooses a summary row from here and details are shown in a DetailsView.  -- %>
        <% '--------------------------------------------------------------------------------------- %>

        <asp:GridView
            ID="GridViewSummary" 
            runat="server" 
            AllowSorting="True" 
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            Width="224px" 
            AllowPaging="True" 
            PageSize="7">

            <Columns>
                <asp:BoundField DataField="Unit" HeaderText="Unit" 
                    SortExpression="Unit" />

                <asp:BoundField DataField="TheName" HeaderText="Name" 
                    SortExpression="TheName" />

                <asp:BoundField DataField="ID" 
                    HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" 
                    Visible="False" />

                <asp:CommandField ButtonType="Button" SelectText="Select Unit Details" 
                    ShowSelectButton="True" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

由于什么都没有发生,我们假设我们需要在代码隐藏文件中编写一些代码。你能告诉我们唤醒排序和分页需要什么编码吗?

【问题讨论】:

  • 你的数据源是什么?您也位于更新面板中。当从部分页面呈现请求中抛出异常时,UpdatePanel 的行为很奇怪。您可以删除 UpdatePanel 进行调试并查看您的代码隐藏是否可能引发异常?
  • DataSource 是从 VB.Net 代码隐藏文件中设置的,使用: Dim theTableAdapter As New DataSetClassesTableAdapters.ClassesTableAdapter Private Sub Teachers_Init(sender As Object, e As EventArgs) Handles Me.Init ' 加载将数据库中的数据导入 GridView。 '---------------------------------------------------- -- GridViewSummary.DataSource = theTableAdapter.GetDataByAllClasses GridViewSummary.DataBind() End Sub
  • 我删除了 UpdatePanel 并显示了此错误: GridView 'GridViewSummary' 触发了未处理的事件 PageIndexChanging。我将添加 Bonny Bonev 建议的编码,因为它使用该事件处理程序。

标签: asp.net vb.net gridview paging gridview-sorting


【解决方案1】:

看看这篇文章 sorting and paging with gridview asp.net

基本上你需要为排序和分页添加服务器端事件处理程序。

这是一个示例 - 您可以复制/粘贴大部分内容。

http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

【讨论】:

  • 感谢您的快速回复。我会看的。
  • 感谢您的加入。我会尝试将其转换为 VB.Net 并让您知道我们是否可以让它工作。
  • 分页工作正常。现在我将进行排序。再次感谢。 :-)
猜你喜欢
  • 1970-01-01
  • 2010-10-16
  • 2012-02-06
  • 2012-08-23
  • 2012-03-14
  • 1970-01-01
  • 2012-04-27
  • 2014-09-14
  • 1970-01-01
相关资源
最近更新 更多