【问题标题】:ASP.Net GridView Paging/Sorting is breakingASP.Net GridView 分页/排序正在中断
【发布时间】:2011-06-23 01:32:25
【问题描述】:

所以我创建了一个网格视图,以及我从指南中学习并适应我的两个网格视图的分页/排序处理程序:

分页

    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ((GridView)sender).PageIndex = e.NewPageIndex; // updatepanel allows this to happen without refreshing

    }

排序

protected void GridView_Sort(object sender, GridViewSortEventArgs e)
        {
            string sortdir;

            ViewState["SortExpression"] = e.SortExpression;

            if (GridViewSortDirection.ToString() == "Ascending")
            {
                sortdir = "ASC";
                GridViewSortDirection = SortDirection.Descending;
            }
            else
            {
                sortdir = "DESC";
                GridViewSortDirection = SortDirection.Ascending;
            }

            var expression = e.SortExpression;

            var gv = sender as GridView;

            DataView gridv = ((DataSet)gv.DataSource).Tables[0].DefaultView; // get GridView datasource as a DataView as it is easier to sort

            gridv.Sort = expression + " " + sortdir; // strict formatted string here; e.g: Subject ASC

            gv.DataSource = gridv;

            gv.DataBind();
        }

    }

我前一阵子一切正常,但现在重新访问该项目,我发现更改页面需要两次单击页码链接而不是一次,并且在更改一页后它将停止响应点击。

gridviews 本身正在填充正确的数据并显示。如果您不能完全分辨出哪里出了问题,任何关于在哪里查看的建议都会很棒。

我还设置了断点来观察事件处理程序是否被调用,并且它们确实如此。

编辑:

<asp:GridView ID="CurrentGridView" runat="server" CssClass="CurrentGridView" 
                                AllowPaging="True" AllowSorting="True"
                                AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
                                 OnSorting="GridView_Sort"
                                GridLines="None" Width="600px" EnableSortingAndPagingCallbacks="True" 
                                 OnPageIndexChanging="GridView_PageIndexChanging" >
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" >
                                    <ItemStyle Width="30px" />
                                    </asp:BoundField>
                                    <asp:HyperLinkField DataNavigateUrlFields="Url" DataTextField="Subject" 
                                        HeaderText="Subject" SortExpression="Subject" >
                                    <ControlStyle CssClass="SubjectColumn" />
                                    <ItemStyle Width="200px" />
                                    </asp:HyperLinkField>
                                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status">
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Created" HeaderText="Logged Date" 
                                        SortExpression="Created" HtmlEncodeFormatString="False">
                                        <ControlStyle CssClass="LoggedDate" />
                                        <ItemStyle HorizontalAlign="Right" Width="80px" />
                                    </asp:BoundField>
                                </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle CssClass="PagerStyle"  ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" VerticalAlign="Top" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            </asp:GridView>

<asp:GridView ID="HistoricalGridView" runat="server" 
                                AutoGenerateColumns="False" CellPadding="4" EnableSortingAndPagingCallbacks="True" EnableModelValidation="True" ForeColor="#333333"

                                GridLines="None" Width="600px">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                                    <asp:HyperLinkField DataNavigateUrlFields="Url" DataTextField="Subject" 
                                        HeaderText="Subject" SortExpression="Subject">
                                    <ControlStyle ForeColor="#5D7B9D" />
                                    </asp:HyperLinkField>
                                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" >
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Created" HeaderText="Logged Date" SortExpression="Created">
                                        <ItemStyle HorizontalAlign="Right" Width="80px" />
                                    </asp:BoundField>
                                </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle CssClass="PagerStyle" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            </asp:GridView>

编辑:我可以看到数据集在到达排序/分页处理程序时被清空,但我没有任何明确的重新初始化它们或清空它们。

【问题讨论】:

  • 你能从 .aspx 中发布 gridview 的标记吗?
  • .ascx 在这种情况下。更新帖子

标签: asp.net visual-studio-2010 gridview


【解决方案1】:

你写道: “编辑:我可以看到数据集在到达排序/分页处理程序时已清空,但我没有任何明确的重新初始化它们或清空它们。”

数据集不会被视图状态或任何东西的网格持久化,每次调用排序或页面时都需要重新绑定。试试看它是否能解决问题。

【讨论】:

  • 嗯,有趣的是它曾经可以工作,但是是的,那是我必须做的。
猜你喜欢
  • 1970-01-01
  • 2012-11-29
  • 2012-02-06
  • 1970-01-01
  • 2010-10-16
  • 2012-08-23
  • 2012-03-14
  • 2011-01-21
  • 1970-01-01
相关资源
最近更新 更多