Pergunta

I have a very simple application that has a GridView. I have a custom BottomPagerRow that is using a drop down list and link buttons. If I attempt to use the controls while the default pages are showing it works fine, but if I change the page size any other changes forces it back to default.

Just looking at this code myself, I can only think that because the row number is changing the ID of the controls are changing and when the server attempts to find them they no longer exist and switch to default.

<asp:GridView ID="dgCQMain" runat="server" EnableViewState="false" PagerSettings-Position="Bottom" OnPageIndexChanging="dgCQMain_PageIndexChanging" AutoGenerateColumns="true" OnRowCreated="dgCQMain_RowCreated">
    <HeaderStyle CssClass="gridHeaderRow" />
    <AlternatingRowStyle CssClass="gridAlternatingRows" />
    <PagerStyle CssClass="gridPager" />
    <RowStyle CssClass="gridRow" />
    <SelectedRowStyle CssClass="gridSelectedRow" />
    <FooterStyle CssClass="gridFooter" />
    <EmptyDataTemplate>
       <asp:Label ID="lblEmptyLaboratoryMain" runat="server" Text="[There are no current items for this patient]"></asp:Label>
       </EmptyDataTemplate>
       <EmptyDataRowStyle CssClass="gridEmpty" />
       <PagerTemplate>
          <table width="100%" cellpadding="0" cellspacing="0" border="0">
             <tr class="gridPager">
                <td class="pagerNumbers">
                   <asp:LinkButton CssClass="pagerNumberLinks" ID="LinkButton1" runat="server" CommandName="Page" CausesValidation="false" CommandArgument="First"><<</asp:LinkButton>
                   |
                   <asp:LinkButton CssClass="pagerNumberLinks" ID="LinkButton2" runat="server" CommandName="Page" CausesValidation="false" CommandArgument="Prev"><</asp:LinkButton>
                   |
                   <asp:Repeater ID="rptPager" runat="server">
                      <ItemTemplate>
                         <asp:LinkButton CssClass="pagerNumberLinks" ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%#Eval("Value") %>' Enabled='<%# Eval("Enabled") %>' OnClick="dgCQMainPage_Changed"></asp:LinkButton>
                         <span>|</span>
                       </ItemTemplate>
                    </asp:Repeater>
                 <asp:LinkButton CssClass="pagerNumberLinks" ID="LinkButton4" runat="server" CommandName="Page" CausesValidation="false" CommandArgument="Next">></asp:LinkButton>
                 |
                 <asp:LinkButton CssClass="pagerNumberLinks" ID="LinkButton5" runat="server" CommandName="Page" CausesValidation="false" CommandArgument="Last">>></asp:LinkButton>
                 |
               </td>
               <td class="gridPager">
                  <asp:Label ID="MessageLabel" Text="Show me" runat="server" />
                  <asp:DropDownList ID="PageDropDownList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="dgCQMainDropDownList_SelectedIndexChanged_Bottom">
                     <asp:ListItem Text="2" />
                     <asp:ListItem Text="5" />
                     <asp:ListItem Text="10" />
                  </asp:DropDownList>
                  <asp:Label ID="Label1" Text=" results per page" runat="server" />
               </td>
            </tr>
         </table>
      </PagerTemplate>
   </asp:GridView>

protected void dgCQMainDropDownList_SelectedIndexChanged_Bottom(Object sender, EventArgs e)
{
   // Set the PageIndex property to display that page selected by the user.
   dgCQMain.PageIndex = 0;
   dgCQMain.PageSize = int.Parse((sender as DropDownList).SelectedItem.Value);
}
Foi útil?

Solução

I found an answer, but I'm not sure I'd say it's the best way to go about it. Here:

/// <summary>
/// Special method to handle the Drop down list value changing 
/// but ASP not accurately modifying the controls
/// </summary>
private void HandlePossibleBottomRowEvents()
{
    var page = associatedGridView.Page;
    var request = page.Request;

    var possibleCall = request.Form["__EventTarget"];
    if (possibleCall != null)
    {
        if (possibleCall.Contains("pagerDDL"))
        {
            var newPageSize = request[possibleCall];
            var newSize = int.Parse(newPageSize);
            if (associatedGridView.PageSize != newSize)
                UpdatePageSize(newSize);
        }
    }
}

This will retrieve the postback cause, check to see if it is one of the two dll's (Top and Bottom row) and set the size to the value of the form post. AssociatedGridView is just the gridview that I'm working with. This is inside of an ITemplate that is acting as a Pager

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top