asp.net GridView绑定与删除时的分页冲突

时间:2014-05-24 09:00:37

标签: c# asp.net gridview paging objectdatasource

我有一个带有ObjectDataSource的Gridview:

<asp:GridView ID="gvCompany" runat="server" SkinID="BasicGridView" DataSourceID="odsCompany" DataKeyNames="ID" OnRowCommand="gvCompany_RowCommand" OnRowDataBound="gvCompany_RowDataBound">
                                    <Columns>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Select %>">
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chbSelect" TabIndex="5" runat="server" CssClass="contrast-checkbox" />
                                            </ItemTemplate>
                                            <ItemStyle Width="50px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                        <asp:BoundField HeaderText="<%$ Resources:Titles,Code %>" DataField="Code" SortExpression="Code">
                                            <ItemStyle Width="10%" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="<%$ Resources:Titles,Name %>" DataField="Name" SortExpression="Name">
                                            <ItemStyle Width="70%" />
                                        </asp:BoundField>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lbtnEdit" TabIndex="7" runat="server" CommandName="EditCompany" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btn btn-contrast btn-xs edit-grid">
                                                <i class="icon icon-edit"></i>
                                                </asp:LinkButton>
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
                                            <ItemTemplate>
                                                <a id="IbtnDelete" tabindex="8" class="btn btn-danger btn-xs edit-grid" onclick="return SetItemForDelete(this);"><i class="icon icon-trash-o"></i></a>
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
                                            <HeaderStyle CssClass="center" />
                                        </asp:TemplateField>
                                    </Columns>
                                    <EmptyDataTemplate>
                                        <%= Resources.Messages.NoCompanyIsForView%>
                                    </EmptyDataTemplate>
                                </asp:GridView>
                                <asp:ObjectDataSource ID="odsCompany" runat="server" EnablePaging="true" TypeName="DMS.Data.CompanyProvider" SelectMethod="Search" SortParameterName="orderby" SelectCountMethod="GetSearchCount" />

当我在GridView中转到页面并删除该页面的所有行时出现问题,在这种情况下,我绑定了Gridview但显示的是EmptyDataTemplate而不是转到另一行有一些行...

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要更改网格的PageIndex属性,因为它不会在重新绑定时自动更改。您可以在此处找到有关PageIndex属性的更多信息:http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.pageindex(v=vs.110).aspx

在你的方法/事件之前,重新绑定重置页面索引,如下所示:

if (gridView.PageIndex >= 1)
    gridView.PageIndex--;

如果你的页面索引是0,那么你想要显示EmptyDataTable。

更新答案: 检查GridView的Rows.Count,仅在当前pageindex中存在一行时更改页面索引:

if (gridView.PageIndex >= 1 && gridView.Rows.Count==1)
    gridView.PageIndex--;