2013-08-06 55 views
1

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

下面是标记:

<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

一些代码和标记可能有帮助! – afzalulh

+0

挑剔挑剔;)见编辑... –

回答

1

我必须设置的EnableViewState = “真”在标记为GridView的是这样的:

<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"> 
+0

太棒了。谢谢。 –

+0

afzalulh:我面临同样的问题,寻呼机不工作。需要一个适当的传呼机与下拉列表。 http://stackoverflow.com/questions/27065077/search-not-working-on-gridview 请帮助这个 – BNN