开发者

ASP.Net GridView Paging/Sorting is breaking

So I've created a gridview, and paging/sorting handlers which I learned from guides and adapted to accomodate my two gridviews :

paging

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

    }

sorting

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开发者_Python百科)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();
        }

    }

I got this all working fine a while ago, but revisiting the project now, I'm finding that changing pages requires two clicks on the page number link rather than one, and after one page change it would stop responding to clicks.

The gridviews themselves are populating with the right data and displaying. Any suggestions on where to look if you can't quite tell what is wrong would be great.

Also I got breakpoints setup to watch if the eventhandlers are being called, and they do.

EDIT :

<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>

and

<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>

EDIT : I can see that the DataSets are nulled by the time they get to the sort/paging handlers, but I don't have anything explicitly reinitializing them or nulling them.


You wrote: "EDIT : I can see that the DataSets are nulled by the time they get to the sort/paging handlers, but I don't have anything explicitly reinitializing them or nulling them."

The dataset is not persisted by the grid in viewstate or anything, you need to rebind everytime you call sort or page. Give that a try and see if it fixes the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜