【问题标题】:Custom Paging for GridView in an UpdatePanel not firing PageIndexChanging eventUpdatePanel 中 GridView 的自定义分页未触发 PageIndexChanging 事件
【发布时间】:2010-03-17 03:14:12
【问题描述】:

我有一个 GridView,它在 UpdatePanel 中使用自定义分页(这样,gridview 的分页和排序不会导致回发)。排序工作正常,但分页没有。永远不会调用 PageIndexChanging 事件。

这是aspx代码:

<asp:UpdatePanel runat="server" ID="upSearchResults" ChildrenAsTriggers="true" UpdateMode="Always">
        <ContentTemplate>
          <asp:GridView ID="gvSearchResults" runat="server" AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" OnDataBound="gvSearchResults_DataBound"
                OnRowDataBound ="gvSearchResults_RowDataBound" OnSorting="gvSearchResults_Sorting" OnPageIndexChanging="gvSearchResults_PageIndexChanging" Width="100%" EnableSortingAndPagingCallbacks="false">
            <Columns>
              <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                  <asp:HyperLink ID="lnkAdd" runat="server">Add</asp:HyperLink>
                  <asp:HiddenField ID="hfPersonId" runat="server" Value='<%# Eval("Id") %>'/>
                </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField HeaderText="First Name" DataField="FirstName" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="FirstName" />
              <asp:BoundField HeaderText="Last Name" DataField="LastName"  HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="LastName" />
              <asp:TemplateField HeaderText="Phone Number" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                  <asp:Label ID="lblPhone" runat="server" Text="" />
                </ItemTemplate>
              </asp:TemplateField>
            </Columns>
            <PagerTemplate>
              <table width="100%" class="pager">
                <tr>
                  <td>
                  </td>
                </tr>
              </table>
            </PagerTemplate>    
          </asp:GridView>
          <div class="btnContainer">
              <div class="btn btn-height_small btn-style_dominant">
                  <asp:LinkButton ID="lbtNewRecord" runat="server" OnClick="lbtNewRecord_Click"><span>Create New Record</span></asp:LinkButton>
              </div>
              <div class="btn btn-height_small btn-style_subtle">
                  <a onclick="openParticipantModal();"><span>Cancel</span></a>
              </div>
          </div>
        </ContentTemplate>
        <Triggers>
          <asp:AsyncPostBackTrigger ControlID="gvSearchResults" EventName="PageIndexChanging" />
          <asp:AsyncPostBackTrigger ControlID="gvSearchResults" EventName="Sorting" />
        </Triggers>
      </asp:UpdatePanel>

在后面的代码中,我有一个在 GridView OnDataBound 事件上调用的 SetPaging 方法:

private void SetPaging(GridView gv)
 {
   GridViewRow row = gv.BottomPagerRow;

   var place = row.Cells[0];

   var first = new LinkButton();
   first.CommandName = "Page";
   first.CommandArgument = "First";
   first.Text = "First";
   first.ToolTip = "First Page";

   if (place != null) place.Controls.Add(first);

   var lbl = new Label();
   lbl.Text = " ";
   if (place != null) place.Controls.Add(lbl);

   var prev = new LinkButton();
   prev.CommandName = "Page";
   prev.CommandArgument = "Prev";
   prev.Text = "Prev";
   prev.ToolTip = "Previous Page";

   if (place != null) place.Controls.Add(prev);

   var lbl2 = new Label();
   lbl2.Text = " ";
   if (place != null) place.Controls.Add(lbl2);

   for (int i = 1; i <= gv.PageCount; i++)
   {
     var btn = new LinkButton();
     btn.CommandName = "Page";
     btn.CommandArgument = i.ToString();

     if (i == gv.PageIndex + 1)
     {
       btn.BackColor = Color.Gray;
     }

     btn.Text = i.ToString();
     btn.ToolTip = "Page " + i.ToString();

     if (place != null) place.Controls.Add(btn);

     var lbl3 = new Label();
     lbl3.Text = " ";
     if (place != null) place.Controls.Add(lbl3);
   }

   var next = new LinkButton();
   next.CommandName = "Page";
   next.CommandArgument = "Next";
   next.Text = "Next";
   next.ToolTip = "Next Page";

   if (place != null) place.Controls.Add(next);

   var lbl4 = new Label();
   lbl4.Text = " ";
   if (place != null) place.Controls.Add(lbl4);

   var last = new LinkButton();
   last.CommandName = "Page";
   last.CommandArgument = "Last";
   last.Text = "Last";
   last.ToolTip = "Last Page";

   if (place != null) place.Controls.Add(last);

   var lbl5 = new Label();
   lbl5.Text = " ";
   if (place != null) place.Controls.Add(lbl5);
 }

如果我不使用自定义分页,分页可以工作,但我确实需要使用自定义分页。我无法弄清楚为什么在使用自定义分页时没有触发 PageIndexChanging 事件。

谢谢,

杰夫

【问题讨论】:

    标签: c# asp.net gridview updatepanel page-index-changed


    【解决方案1】:

    嗯....我对自定义分页不太熟悉,但是...当一个事件没有为数据绑定控件触发时,它通常是以下两种情况之一:

    1) 您在错误的时间重新绑定数据。 2) 回发页面上的控制层级不相同。

    由于您是动态创建此控件,因此我将在此处使用 #2。事实上,您可能会遇到这两个问题的组合,因为您是在 OnDataBound 事件中创建控件。这意味着该控件的唯一创建时间是在您调用 DataBind 时,您不应该在回发时这样做,直到您处理了这些事件之后。所以你在这里给自己编码了一点 Catch-22。

    是否可以通过标记添加自定义寻呼机?这将解决您的问题,因为这样您就不会依赖这个动态创建的控件。您的另一个选择是将动态控件创建移至 Init 事件。

    【讨论】:

    • 我更喜欢动态创建寻呼机,因为页面上有多个 GridView。我尝试将调用 SetPaging(GridView gv) 从 gvSearchResults_DataBound 事件移动到 gvSearchResults_Init 事件,但这会导致 var place = row.Cells[0];因为 gv.BottomPagerRow 为空。
    • 更新 - 在没有 UpdatePanel 的页面上,自定义分页有效,当我查看源代码时我有这个:2 在更新面板上,我得到: 2 它掉了命令和参数('Page$2')。
    • 啊。什么,UpdatePanel 引起了问题?谁会想到...我很确定 UpdatePanel 负责 StackOverflow 上 50% 的 ASP.NET 问题。 ctl13$ctl06 是什么?那是面板 ID 吗?
    【解决方案2】:

    如果您动态创建 GridView,它将为您省去很多麻烦:请参阅 this link 例如。您只需要一个数据集,其余的将由控件完成...

    【讨论】:

      【解决方案3】:

      您的代码是否有任何 Response.Write 或 Response.WriteLine?如果有,请删除它。因为它也有效果。 :D

      【讨论】:

      • 迈克尔,欢迎来到 stackoverflow。 :) 无需请人访问您的网站,它已在您的profile page 中列出,任何想查看的人都可以在那里找到。
      【解决方案4】:

      其他事件,例如gvSearchResults_Sorting 是否正确触发?如果没有,请查看页面上的 AutoEventWireup 属性,它应该设置为 true,因为您没有显式绑定事件处理程序。

      否则,您是否尝试过没有UpdatePanel 的问题?

      【讨论】:

        【解决方案5】:

        在绑定 GridView 之前,请执行以下操作:

        GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);
        

        【讨论】:

        • PageIndexChanging 事件已经绑定在 ASP 代码中。
        猜你喜欢
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多