开发者

Custom Paging for GridView in an UpdatePanel not firing PageIndexChanging event

I have a GridView that uses custom paging inside an UpdatePanel (so that the paging and sorting of the gridview don't cause postback). The sorting works fine, but the paging doesn't. The PageIndexChanging event is never called.

This is the aspx code:

<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>

In the code behind I have a SetPaging method that is called on the GridView OnDataBound event:

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

The paging works if I don't use custom paging, but I really need to use the custom paging. I can't figure out why the PageIndexChanging event isn't fired when I'm using the custom paging.

Thanks,

Jeff


Well.... I'm not too familiar with custom paging, but... when an event does not fire for a databound control it's generally one of two things:

1) You are re-databinding at the incorrect time. 2) The control heirarchy is not identical on the postback page.

Since you are creating this control dynamically, I'm gonna go with #2 here. In fact you might have a combination of these two problems, since you are creating the controls in the OnDataBound event. That means the only time this control is created is when you call DataBind, which you should not do on postback until after you've handled the events. So you've coded yourself a little Catch-22 here.

Isn't it possible to add a custom pager via markup? That would fix your problem as then you wouldn't be relying on this dynamically created control. Your other option would be to move the dynamic control creation to the Init event.


If you create the GridView dynamically it will save you a lot of hassle : see this link E.g. all you need is a DataSet and the rest will be done by the control ...


Do you have any Response.Write or Response.WriteLine to your code? Kindly remove it if you have. Because it has an effect also. :D


Are the other events, like gvSearchResults_Sorting fired correctly? If not, look at the AutoEventWireup property on your page, it should be set to true because you're not explicitly binding event handlers.

Otherwise, have you tried your problem without the UpdatePanel?


Before you bind the GridView do this:

GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜