无法在GridView的BottomPagerRow中找到控件

时间:2013-02-21 04:55:00

标签: c# asp.net gridview custompaging

我有一个非常简单的应用程序,它有一个GridView。我有一个使用下拉列表和链接按钮的自定义BottomPagerRow。如果我在默认页面显示时尝试使用控件,它可以正常工作,但如果我更改页面大小,任何其他更改都会强制它恢复为默认页面。

我自己只看这段代码,我只能认为因为行号正在改变控件的ID正在改变,当服务器试图找到它们时它们不再存在并切换到默认值。

<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);
}

1 个答案:

答案 0 :(得分:0)

我找到了答案,但我不确定我会说这是最好的解决方法。这里:

/// <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);
        }
    }
}

这将检索回发原因,检查它是否是两个dll之一(顶部和底部行)并将大小设置为表单帖子的值。 AssociatedGridView只是我正在使用的gridview。这是一个充当寻呼机的ITemplate内部