GridView设置页面大小下拉页脚

时间:2013-08-06 15:51:03

标签: asp.net gridview updatepanel

我在更新面板中有一个网格视图。在网格视图的页脚中,我有一个下拉列表,允许用户选择每页的记录数。用户第一次使用下拉列表选择页面大小时,它可以正常工作。在此之后,所选索引更改的下拉事件仅在每隔一次触发。因此,下拉列表的奇数编号选择,所选索引更改了火灾,偶数编号时间,所选索引更改未触发,下拉列表恢复为第一个选项,并且网格视图加载了该记录数。

这是标记:

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
        <ContentTemplate>
            <asp:ObjectDataSource ID="objectDataSourceResults" runat="server" SortParameterName="sortExpression">
                <SelectParameters>
                    <asp:Parameter Name="startIndex" Type="Int32" />
                    <asp:Parameter Name="pageSize" Type="Int32" />
                    <asp:Parameter Name="selectExpression" Type="String" />
                    <asp:Parameter Name="sortExpression" Type="String" />
                    <asp:Parameter Name="includeProperties" Type="String" />
                    <asp:Parameter Name="filterExpression" Type="String" />
                    <asp:Parameter Name="filterArray" Type="Object" />
                </SelectParameters>
            </asp:ObjectDataSource>
            <asp:GridView ID="gridResults" runat="server" AllowPaging="True" AllowSorting="True"  PageSize="25"
                EnableViewState="false" CssClass="Grid" EmptyDataRowStyle-CssClass="GridEmpty"
                ShowHeaderWhenEmpty="false" EmptyDataRowStyle-BorderStyle="None" DataSourceID="objectDataSourceResults"
                AutoGenerateColumns="False" EmptyDataText="No records returned">
                <EmptyDataTemplate>
                    <asp:Label ID="Label1" runat="server" Text="No records found" style="color:#FF0000"></asp:Label>
                </EmptyDataTemplate>
                <EmptyDataRowStyle CssClass="GridEmpty" />
                <AlternatingRowStyle CssClass="AltRow" />
                <SortedAscendingHeaderStyle CssClass="GridHeaderAscending" />
                <SortedDescendingHeaderStyle CssClass="GridHeaderDescending" />
                <PagerTemplate>
                    <table cellpadding="0" width="100%" cellspacing="0" class="GridPager">
                        <tr>
                            <td style="width: 30%; text-align: left; padding-left: 5px; padding-right: 5px">
                                <asp:ImageButton ID="buttonFirst" AlternateText="First Page" CommandArgument="0"
                                    Style="margin-top: 5px;" ImageUrl="~/controls/Images/grid_paging_first.ico" runat="server" />
                                <asp:ImageButton ID="buttonPrevious" AlternateText="" CommandArgument="prev" ImageUrl="~/controls/Images/grid_paging_previous.ico"
                                    runat="server" />
                                <asp:TextBox ID="SetPage" Width="27" MaxLength="3" AutoPostBack="true" runat="server" style="text-align:center;"
                                    CssClass="input-text"></asp:TextBox>
                                <asp:ImageButton ID="buttonNext" AlternateText="" CommandArgument="next" ImageUrl="~/controls/Images/grid_paging_next.ico"
                                    runat="server" />
                                <asp:ImageButton ID="buttonLast" AlternateText="Last Page" CommandArgument="last"
                                    ImageUrl="~/controls/Images/grid_paging_last.ico" runat="server" />
                            </td>
                            <td style="width: 20%; text-align: center;">
                                <asp:Label ID="Label2" runat="server" Text="Per Page:"></asp:Label>
                                <asp:DropDownList ID="dropDownRecordsPerPage" runat="server" CssClass="text-input" OnInit="dropDownRecordsPerPage_Init" 
                                    AutoPostBack="true" OnSelectedIndexChanged="dropDownRecordsPerPage_SelectedIndexChanged" AppendDataBoundItems="true"
                                    Style="text-align: right;">
                                    <asp:ListItem Value="10" Text="10" />
                                    <asp:ListItem Value="25" Text="25" />
                                    <asp:ListItem Value="50" Text="50" />
                                    <asp:ListItem Value="100" Text="100" />
                                    <asp:ListItem Value="1000" Text="All" />
                                </asp:DropDownList>
                            </td>
                            <td style="width: 20%; text-align: center;">
                                <asp:Label ID="labelPageCount" runat="server" />
                            </td>
                            <td style="width: 30%; text-align: right; padding-right: 5px;">
                                <asp:Label ID="labelRecordCount" runat="server" /><br />
                            </td>
                        </tr>
                    </table>
                </PagerTemplate>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

不确定要包含哪些代码。我设置下拉项目:

''' <summary>
''' Create version to refernce in code
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Protected Sub dropDownRecordsPerPage_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    PageSizeDropDown = sender
    PageSizeDropDown.SelectedValue = gridResults.PageSize
End Sub 

所选索引改变了事件:

 ''' <summary>
''' Reload the grid when results per page is changed
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Sub dropDownRecordsPerPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    'Load the results
    gridResults.PageSize = PageSizeDropDown.SelectedValue
End Sub

1 个答案:

答案 0 :(得分:1)

我必须在GridView的标记中设置 EnableViewState =“true”,如下所示:

<asp:GridView ID="gridResults" runat="server" AllowPaging="True" AllowSorting="True"  PageSize="25"
                EnableViewState="true" CssClass="Grid" EmptyDataRowStyle-CssClass="GridEmpty"
                ShowHeaderWhenEmpty="false" EmptyDataRowStyle-BorderStyle="None" DataSourceID="objectDataSourceResults"
                AutoGenerateColumns="False" EmptyDataText="No records returned">